Mark As Completed Discussion

Building a RESTful API with Express.js

To build a RESTful API with Express.js, follow these steps:

  1. Install Express.js by running the following command:
SNIPPET
1npm install express
  1. 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});
  1. 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.

JAVASCRIPT
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment