DEV Community

Boutheina Remadi
Boutheina Remadi

Posted on

Creating a Node.js API with Express (Students Example)

Introduction
In this article, we will build a simple Node.js API using Express.
The goal is not to create a complex application, but to understand the basic structure of a professional backend.

This example will later be extended for an educational platform, where students can study law and social sciences.

Technologies Used
Node.js

Express

CORS

JSON

A database connection (abstracted for now)

Project Structure
Here is a minimal and scalable project structure:

This structure helps keep the code clean and easy to maintain.

🧩 Express Server Code

Below is the main file of our Express application:

Code Explanation
1️⃣ Express Initialization
const app = express();

This creates an Express application instance.

2️⃣ Middleware Configuration
app.use(cors());

Middleware Configuration
app.use(cors());
app.use(express.json());
CORS allows requests from external frontends.

express.json() enables the server to read JSON request bodies.
3️⃣ Database Connection
connectDB();

This function connects the application to a database when the server starts.

Students API Routes
app.use("/api/students", studentsRouter);

All student-related routes are available under:

GET /api/students

POST /api/students

GET /api/students/:id
PUT /api/students/:id

DELETE /api/students/:id

This follows REST API best practices.
Starting the Server
app.listen(5000, () =>
console.log("API running on http://localhost:5000")
);

The server runs on port 5000.
Why This Setup Is a Good Starting Point

✔️ Clean and readable
✔️ Beginner-friendly
✔️ Easy to extend
✔️ Frontend-agnostic
✔️ Suitable for educational projects

What’s Next?

In the next articles, we will:

implement full CRUD operations for students

connect a real database

add courses and legal references

build an educational library

secure the API
✨ Conclusion

With just a few lines of code, we have created a solid Express backend foundation.
This setup is perfect for learning Node.js and building real-world projects step by step.
📌 Follow the series

More tutorials coming soon 🚀

Top comments (0)