How to Serve Both API Website and API Under the Same URL
// serve the static website from public folder
app.use("/", express.static(path.join(__dirname, "public")));
app.get("/?", (req, res) => { // retrieve user requested data and send back using res})

Final Thoughts

As I mentioned in above code, the order matters: the middleware function should come before serving the static web pages. Otherwise the middleware function won’t work.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Wenhe Qi

Wenhe Qi


A lifelong learner who has enthusiasm for sharing knowledge; a developer who loves bringing ideas to life.