APIs are the backbone of modern web applications.
Whether you’re building a single-page app, a mobile interface, or a microservice, a clean and scalable API structure is essential for maintainability and growth. In this article, we’ll walk through how to create a REST API using Node.js and Express, applying best practices in organization, routing, error handling, and modularization.
You’ll learn how to start from scratch and end up with a well-structured, production-ready API that can grow with your application. Once your API is ready, you can easily deploy it on a cloud platform like Galaxy, ensuring scalability and reliable performance.
This tutorial is ideal for beginners and intermediate developers looking to improve their backend skills.
Project Setup
Let’s initialize a new Node.js project and install the required dependencies:
mkdir node-api-galaxy
cd node-api-galaxy
npm init -y
npm install express dotenv
Your file structure should look like this:
node-api-galaxy/
├── controllers/
├── routes/
├── .env
├── index.js
Creating the Express Server
Create the index.js file and add the basic setup:
const express = require('express');
require('dotenv').config();
const app = express();
app.use(express.json());
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('API is running');
});
app.listen(PORT, () => {
console.log(`Server is listening on port ${PORT}`);
});Add a .env file with:
PORT=4000Organizing Routes and Controllers
Let’s create modular files for our routes and logic.
///routes/userRoutes.js
const express = require('express');
const router = express.Router();
const { getUsers, createUser } = require('../controllers/userController');
router.get('/', getUsers);
router.post('/', createUser);
module.exports = router;///controllers/userController.js
exports.getUsers = (req, res) => {
res.status(200).json({ message: 'List of users' });
};
exports.createUser = (req, res) => {
const user = req.body;
res.status(201).json({ message: 'User created', data: user });
};
And import into index.js:
const userRoutes = require('./routes/userRoutes');
app.use('/api/users', userRoutes);Now your API has the route /api/users with GET and POST functionality.
Middleware and Error Handling
Let’s create a basic error handler.
//middleware/errorMiddleware.js
function errorHandler(err, req, res, next) {
console.error(err.stack);
res.status(500).json({ error: 'Internal Server Error' });
}
module.exports = errorHandler;Then import it in index.js:
const errorHandler = require('./middleware/errorMiddleware');
app.use(errorHandler);Best Practices Summary
- Use express.Router() for clean routing
- Keep controllers separate from routes
- Use .env for environment configuration
- Implement middleware for consistent error handling
- Keep code modular for scalability
Conclusion
In this article, you learned how to build and structure a REST API using Node.js and Express with best practices in mind. These techniques will help you scale your API, improve code maintainability, and prepare for real-world production deployments. Whether you plan to deploy on Galaxy or another cloud platform, this approach provides a solid backend foundation.