How to Serve Both API Website and API Under the Same URL

https://www.dummy.com/
https://www.dummy.com/?key=API_KEY&field1=value1&field2=value2
// 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

13 Followers

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