When I first dove into web development, one of the first frameworks I came across was Express.js. It’s a minimalist web framework for Node.js that simplifies the process of building robust web applications. After spending some time getting familiar with it, I wanted to share my insights and essentials to help you get started with Express.js.

Why Express.js?

  • Minimalist: Express.js is light and unopinionated, allowing you to build applications your way.
  • Middleware: It supports middleware, which is a key aspect of its architecture. You can add functionality at various stages of request processing.
  • Robust Routing: The framework provides a powerful routing mechanism, making it easy to handle different HTTP routes for your application.
  • Template Engines: With Express.js, integrating template engines (like EJS or Pug) is straightforward, allowing you to serve dynamic HTML content.

Setting Up Express.js

  1. Install Node.js: Make sure to have Node.js installed. You can download it here.
  2. Create a New Project:
    mkdir express-app
    cd express-app
    npm init -y
    
  3. Install Express:
    npm install express
    

Building Your First Express.js Server

Creating a server with Express is pretty simple. Here’s a basic example:

const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;

app.get('/', (req, res) => {
    res.send('Hello World!');
});

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

Running Your Server

In your terminal, run the following command:

node index.js

This will start your server, and you can navigate to http://localhost:3000 in your web browser. You should see “Hello World!” displayed.

Middleware

Middleware functions are essential in Express.js. They are executed in sequence for every request. I commonly use the following:

  • Body Parser: To parse incoming request bodies.
    npm install body-parser
    

    Usage:

    const bodyParser = require('body-parser');
    app.use(bodyParser.json());
    
  • Logger: A simple logging middleware that I find useful.
    const morgan = require('morgan');
    app.use(morgan('dev'));
    

Routing

Express.js makes it easy to handle different endpoints. Creating a router is as simple as:

const router = express.Router();

router.get('/users', (req, res) => {
    res.json([{ id: 1, name: "John Doe" }]);
});

app.use('/api', router);

Error Handling

Don’t forget to implement error handling. Here’s a simple way to catch errors in your application:

app.use((err, req, res, next) => {
    console.error(err.stack);
    res.status(500).send('Something broke!');
});

Conclusion

Express.js is a fantastic framework for building web applications with Node.js. Its straightforward API and middleware architecture provide a solid foundation for building anything from simple websites to complex APIs.

Feel free to customize your server and explore more advanced features such as authentication, database integration, or deploying to services like Heroku or Vercel. Happy coding!

For further reading and advanced concepts, be sure to check out the official Express documentation.

Find more of my blogs at https://nadbn.com/blog