Building a RESTful API with Express.js
To build a RESTful API with Express.js, follow these steps:
- Install Express.js by running the following command:
SNIPPET
1npm install express- Create an Express.js application and define a route for the API endpoint. For example:
JAVASCRIPT
1const express = require("express");
2const app = express();
3
4app.get("/api", (req, res) => {
5 res.json({ message: "Hello, world!" });
6});- Start the Express.js server and listen on a specific port. For example:
JAVASCRIPT
1const PORT = 3000;
2
3app.listen(PORT, () => {
4 console.log(`Server is running on port ${PORT}`);
5});Once your Express.js API is up and running, you can test it by making HTTP requests to the defined endpoints.
xxxxxxxxxx14
// Setting up Express.jsconst express = require("express");const app = express();const PORT = 3000;app.get("/api", (req, res) => { res.json({ message: "Hello, world!" });});app.listen(PORT, () => { console.log(`Server is running on port ${PORT}`);});OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment


