187k views
1 vote
How to create rest api in express js

User Eldar
by
7.2k points

1 Answer

0 votes

Final answer:

To create a REST API in Express.js, you need to: install dependencies, import modules, define routes and handlers, and start the server. Here's an example:

Step-by-step explanation:

To create a REST API in Express.js, you will need to follow a few steps:

Here's an example of creating a simple REST API in Express.js:

// Import required modules
const express = require('express');
const bodyParser = require('body-parser');

// Create an instance of Express
const app = express();

// Use body-parser middleware
app.use(bodyParser.json());

// Define routes
app.get('/api/users', (req, res) => {
// Handle GET request to /api/users
// Return JSON response
res.json({
message: 'GET request to /api/users'
});
});

// Start the server
const port = 3000;
app.listen(port, () => {
console.log(`Server is listening on port ${port}`);
});
User Vilson
by
7.5k points