My Blog
Latest Articles & Resources
Node.js & Express
How to Build a REST API with Express.js in 30 Minutes and Deploy it on Hostinger
Building a robust REST API is a fundamental skill for any MEAN stack or backend developer. In this guide, I will show you how to build a fully functional REST API using Node.js and Express.js from scratch in under 30 minutes, and then we will deploy it live using Hostinger's powerful environment.
Prerequisites
Node.js installed on your machine.
A code editor like VS Code.
Basic understanding of JavaScript.
Step 1: Initialize Your Project
First, create a new directory for your project and navigate into it. Then, initialize a new Node project:
mkdir express-api
cd express-api
npm init -y
Step 2: Install Dependencies
We only need a few packages to get started. Express for the server and Nodemon for auto-restarting the server during development.
npm install express
npm install --save-dev nodemon
Step 3: Setup the Server
Create an index.js file in the root folder. This will be the entry point of our API.
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware to parse JSON
app.use(express.json());
// Basic Route
app.get('/', (req, res) => {
res.json({ message: "Welcome to my API!" });
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Step 4: Create RESTful Routes
Now, let's create a simple in-memory array of users to perform CRUD (Create, Read, Update, Delete) operations.
let users = [
{ id: 1, name: "Jeel Faldu", role: "Developer" },
{ id: 2, name: "John Doe", role: "Designer" }
];
// GET all users
app.get('/api/users', (req, res) => {
res.json(users);
});
// POST a new user
app.post('/api/users', (req, res) => {
const newUser = {
id: users.length + 1,
name: req.body.name,
role: req.body.role
};
users.push(newUser);
res.status(201).json(newUser);
});
Test your API locally by running npx nodemon index.js. Your endpoints are now live at http://localhost:3000/api/users.
Step 5: Deploying on Hostinger (Highly Recommended)
Once your API is ready, you need a reliable hosting provider to push it live. I personally use and highly recommend Hostinger for all my Node.js and MEAN stack projects. They offer an intuitive hPanel, incredible speeds, and excellent pricing for developers.
🔥 Special Developer Discount
If you're looking to host your web apps, APIs, or databases, Hostinger is currently running an amazing offer.
Claim Hostinger Discount Now →
To deploy:
Zip your project folder (excluding the node_modules folder).
Log in to your Hostinger Account and go to your File Manager.
Upload the zip file and extract it.
Use Hostinger's Node.js Setup tool (in hPanel) to configure your App's entry point to index.js.
Click start, and your server will install dependencies and go live!
Conclusion
And there you have it! You've successfully built and deployed an Express.js REST API. Let me know in the comments or via my contact page if you have any questions.
Read Article