Sitemap

Building a Simple REST API with Node.js

Sep 27, 2024

--

In this guide, we’ll create a basic REST API using Node.js and Express.

Get Smalery Suffy’s stories in your inbox

Join Medium for free to get updates from this writer.

Setup:

  1. Create a new directory: mkdir my-api && cd my-api
  2. Initialize the project: npm init -y
  3. Install Express: npm install express

Create server.js:

javascript
Copy code
const express = require('express');
const app = express();
const PORT = 3000;
app.use(express.json());
app.get('/api/items', (req, res) => res.json([{ id: 1, name: 'Item 1' }]));
app.listen(PORT, () => console.log(`Server running on http://localhost:${PORT}`));

Test with Postman using GET /api/items. Happy coding!

--

--

Smalery Suffy
Smalery Suffy

Written by Smalery Suffy

0 followers

I'm a professional developer who loves building websites and apps. I enjoy solving problems with code and always learning new things in technology.