Building a User REST API with Node.js and Express

Building a User REST API with Node.js and Express

                 Creating REST APIs is a core part of modern web development. Node.js and Express make it particularly easy to set up these APIs. This blog will guide you through building a simple REST API for user management with methods to retrieve (GET) and add (POST) users.

Setting Up Your Project

     Before diving into the code, ensure you have Node.js installed. Then, set up a new project:

1.Initialize a new Node.js project:

				
					mkdir user-api
cd user-api
npm init -y

				
			

2.Install Express and CORS middleware:

				
					npm install express cors

				
			

Creating the REST API

Here’s how to build a simple REST API to manage users:

Step 1: Basic Setup

Start by setting up Express and adding some middleware:

				
					const express = require('express');
const cors = require('cors');

const app = express();
app.use(cors()); // Enables CORS
app.use(express.json()); // Parses incoming JSON requests and puts the parsed data in req.body

				
			

Step 2: Define User Data

For demonstration purposes, use a simple array to store user data:

				
					let users = [
    { id: 1, name: 'John Doe', email: 'john@example.com', phone: '1234567890', password: '$A123456'},
    { id: 2, name: 'Jane Doe', email: 'jane@example.com', phone: '0987654321', password: '$B123456'},
    { id: 3, name: 'Jim Doe', email: 'jim@example.com', phone: '1234567890', password: '$C123456'}
];

				
			

Step 3: Implement GET Endpoint

Create a GET endpoint to retrieve all users:

				
					app.get('/api/users', (req, res) => {
    res.json(users); // Sends the current array of users as JSON
});

				
			

Step 4: Implement POST Endpoint

Add a POST endpoint to create new users:

				
					app.post('/api/users', (req, res) => {
    const { name, email, phone, password } = req.body;
    const id = users.length + 1; // Generate a new ID
    users.push({ id, name, email, phone, password }); // Add new user to array
    res.status(201).json({ id, name, email, phone, password }); // Send back the created user data
});

				
			

Step 5: Start the Server

Set up the server to listen on port 5000:

				
					app.listen(5000, () => {
    console.log('Server is running on port 5000');
});

				
			

Testing the API

To test your API, you can use tools like Postman or cURL. Here’s how you might test it using cURL:

  • GET Users:

				
					curl http://localhost:5000/api/users

				
			

POST User:

				
					curl -X POST http://localhost:5000/api/users \
-H 'Content-Type: application/json' \
-d '{"name": "Alice Doe", "email": "alice@example.com", "phone": "1234567890", "password": "$D123456"}'

				
			

By following these steps, we  created a simple yet functional REST API for managing users using Node.js and Express. This API includes endpoints to retrieve and add users, showcasing how straightforward it is to set up basic backend functionalities with modern JavaScript tools.

Overview

Leave a Reply

Your email address will not be published. Required fields are marked *

Enquire Now