๐Ÿš€ Building a Simple REST API with Node.js & Express โ€“ A Step-by-Step Guide

APIs are the backbone of contemporary online applications, providing smooth communication between the frontend and backend.

In this post, we’ll step through constructing a basic REST API using Node.js & Express from scratch. Letโ€™s get started! ๐Ÿš€


This content originally appeared on DEV Community and was authored by Erasmus Kotoka

APIs are the backbone of contemporary online applications, providing smooth communication between the frontend and backend.

In this post, we'll step through constructing a basic REST API using Node.js & Express from scratch. Letโ€™s get started! ๐Ÿš€

๐Ÿ“Œ Step 1: Setup Your Node.js Project

First, verify you have Node.js installed. Then, initialize a new project:

sh Copy Edit mkdir simple-api && cd simple-api npm init -y

This produces a package.json file, which maintains dependencies.

๐Ÿ“Œ Step 2: Install Express.js

Express is a lightweight and quick framework for creating web applications in Node.js. Install it:

sh Copy Edit npm install express ๐Ÿ“Œ Step 3: Create Your Server

Create a new file server.js and add this code:

javascript Copy Edit const express = require('express'); const app = express(); const PORT = 3000;

app.get('/', (req, res) => { res.send('Hello, API! ๐Ÿš€'); });

app.listen(PORT, () => { console.log(Server is operating on http://localhost:${PORT}); });

Whatโ€™s Happening?

โœ” express() initializes an Express app.

โœ” app.get('/') handles GET queries to the root URL.

โœ” app.listen(PORT) launches the server.

Run it with:

sh

Copy Edit node server. js

Visit http://localhost:3000 in your browserโ€”you should see "Hello, API! ๐Ÿš€".

๐Ÿ“Œ Step 4: Add More Routes

Letโ€™s build routes to handle CRUD actions (Create, Read, Update, Delete). Modify server. js:

javascript Copy Edit app.use(express.json()); // Middleware to parse JSON

const users = [{ id: 1, name: 'John Doe' }] ;

// Get all users app. get('/users', (req, res) => { res.json(users); });

// Add a new user app. post('/users', (req, res) => { const newUser = { id: users.length + 1, ...req.body }; users.push(newUser); res.status(201).json(newUser); });

Now you can:

โœ” GET /users โ†’ Retrieve all users

โœ” POST /users โ†’ Add a new user

Test with Postman or cURL:

sh Copy Edit curl -X POST -H "Content-Type: application/json" -d '{"name": "Jane Doe"}' http://localhost:3000/users ๐Ÿ“Œ Step 5: Handling Errors

Letโ€™s add error handling to enhance our API:

javascript Copy Edit app.use((req, res) => { res.status(404).json({ message: 'Route not found!' }); });

Now, if a user accesses an incorrect endpoint, they receive a 404 error instead of a blank answer.

๐Ÿ“Œ Step 6: Running and Testing 1๏ธโƒฃ Start the server:

sh Copy Edit node server. js

2๏ธโƒฃ Open http://localhost:3000/users in your browser or Postman to test.

3๏ธโƒฃ Use Postman to submit a POST request to /users.

๐Ÿš€ Next Steps ๐Ÿ”น Add MongoDB with Mongoose for data storage. ๐Ÿ”น Implement JWT authentication for security. ๐Ÿ”น Deploy the API using Render, Vercel, or Heroku.

By following these instructions, youโ€™ve constructed a completely functioning REST API using Node.js & Express! ๐ŸŽ‰

๐Ÿ’ฌ Have questions? Drop them in the comments!

#NodeJS #ExpressJS #RESTAPI #Backend #WebDevelopment


This content originally appeared on DEV Community and was authored by Erasmus Kotoka


Print Share Comment Cite Upload Translate Updates
APA

Erasmus Kotoka | Sciencx (2025-03-02T20:00:31+00:00) ๐Ÿš€ Building a Simple REST API with Node.js & Express โ€“ A Step-by-Step Guide. Retrieved from https://www.scien.cx/2025/03/02/%f0%9f%9a%80-building-a-simple-rest-api-with-node-js-express-a-step-by-step-guide/

MLA
" » ๐Ÿš€ Building a Simple REST API with Node.js & Express โ€“ A Step-by-Step Guide." Erasmus Kotoka | Sciencx - Sunday March 2, 2025, https://www.scien.cx/2025/03/02/%f0%9f%9a%80-building-a-simple-rest-api-with-node-js-express-a-step-by-step-guide/
HARVARD
Erasmus Kotoka | Sciencx Sunday March 2, 2025 » ๐Ÿš€ Building a Simple REST API with Node.js & Express โ€“ A Step-by-Step Guide., viewed ,<https://www.scien.cx/2025/03/02/%f0%9f%9a%80-building-a-simple-rest-api-with-node-js-express-a-step-by-step-guide/>
VANCOUVER
Erasmus Kotoka | Sciencx - » ๐Ÿš€ Building a Simple REST API with Node.js & Express โ€“ A Step-by-Step Guide. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/03/02/%f0%9f%9a%80-building-a-simple-rest-api-with-node-js-express-a-step-by-step-guide/
CHICAGO
" » ๐Ÿš€ Building a Simple REST API with Node.js & Express โ€“ A Step-by-Step Guide." Erasmus Kotoka | Sciencx - Accessed . https://www.scien.cx/2025/03/02/%f0%9f%9a%80-building-a-simple-rest-api-with-node-js-express-a-step-by-step-guide/
IEEE
" » ๐Ÿš€ Building a Simple REST API with Node.js & Express โ€“ A Step-by-Step Guide." Erasmus Kotoka | Sciencx [Online]. Available: https://www.scien.cx/2025/03/02/%f0%9f%9a%80-building-a-simple-rest-api-with-node-js-express-a-step-by-step-guide/. [Accessed: ]
rf:citation
» ๐Ÿš€ Building a Simple REST API with Node.js & Express โ€“ A Step-by-Step Guide | Erasmus Kotoka | Sciencx | https://www.scien.cx/2025/03/02/%f0%9f%9a%80-building-a-simple-rest-api-with-node-js-express-a-step-by-step-guide/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.