
Node.js Tutorial – Day 7: Getting Started with Express.js
Node.js has become a cornerstone of modern web development, allowing developers to build fast and scalable server-side applications. In this Node.js Tutorial – Day 7, we will introduce Express.js, one of the most popular frameworks for Node.js. Express.js simplifies building web applications and APIs by providing a robust set of features. Whether you are a beginner or looking to enhance your Node.js skills, learning Express.js is essential for modern backend development.
Why Use Express.js with Node.js?
Node.js itself provides a way to create servers and handle requests, but building complex applications with pure Node.js can be cumbersome. Express.js acts as a web framework that streamlines routing, middleware, and handling HTTP requests. Some of the key benefits include:
- Minimalistic and flexible: Allows building both small and large applications.
- Middleware support: Easily add functionalities like authentication, logging, and error handling.
- Routing made easy: Create clean and readable routes for different endpoints.
- Large ecosystem: Integrates with many packages via npm.
You can check out the official Express.js documentation here for more detailed guidance.
Setting Up Express.js
Before starting, ensure that you have Node.js and npm installed on your system. You can verify installation using:
node -v
npm -v
Now, create a new project folder and initialize npm:
mkdir express-demo
cd express-demo
npm init -y
Next, install Express.js:
npm install express
After installation, we can create a basic server. Create a file named app.js
:
const express = require('express');
const app = express();
const PORT = 3000;
// Basic route
app.get('/', (req, res) => {
res.send('Welcome to Node.js Tutorial Day 6: Express.js');
});
// Start server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
Run the server with:
node app.js
Visit http://localhost:3000
in your browser to see your first Express.js response.
Pro Tip: Always keep your server code modular. For larger projects, split routes and middleware into separate files.
Routing in Express.js
Express.js routing allows you to handle multiple HTTP requests in a clean and organized manner. For example, let’s create a few more routes:
app.get('/about', (req, res) => {
res.send('About Page - Node.js Tutorial Day 6');
});
app.get('/contact', (req, res) => {
res.send('Contact Page - Node.js Tutorial Day 6');
});
Now, visiting /about
or /contact
will display the respective messages. Express makes it simple to manage routes for larger applications.
Using Middleware in Express.js
Middleware functions in Express.js process requests before sending a response. They are perfect for logging, authentication, or parsing request bodies. Here’s a simple example:
// Logger middleware
app.use((req, res, next) => {
console.log(`${req.method} request to ${req.url}`);
next();
});
This middleware will log all incoming requests to the console. Express also provides built-in middleware like express.json()
for parsing JSON bodies in POST requests.
Next Steps
Now that you have a basic Express.js server running, try these exercises:
- Create a route
/products
that returns a list of product objects in JSON format. - Use
express.json()
to handle POST requests and save data to an in-memory array. - Explore third-party middleware like
cors
andmorgan
for better functionality.
For a deeper dive, you can also explore building a REST API using Node.js and Express.js with MongoDB integration in upcoming blogs.
Conclusion
In this Node.js Tutorial – Day 7, we learned how to get started with Express.js, including:
- Setting up an Express.js project
- Creating routes
- Using middleware for request handling
Express.js is a must-learn framework for Node.js developers. It simplifies backend development and allows you to focus on building scalable applications without reinventing the wheel.
What’s Next?
In the next blog, Node.js Tutorial – Day 8: Node.js MongoDB Integration, we will explore how to connect your Express.js application to a MongoDB database. You’ll learn how to:
- Set up MongoDB locally or using MongoDB Atlas
- Create Mongoose schemas and models for your data
- Perform CRUD operations (Create, Read, Update, Delete)
- Test your API endpoints with Postman
This will allow you to build fully functional web applications with a persistent database, taking your Node.js skills to the next level.