DEV Community

Romulo Gatto
Romulo Gatto

Posted on

Express.js: Introduction and Basic Routing

Express.js: Introduction and Basic Routing

Welcome to the world of Express.js! In this guide, we'll introduce you to the basics of Express.js and walk you through how to set up routing for your web application. Whether you're a beginner or an experienced developer, understanding these fundamentals will help you build powerful and scalable Node.js applications.

What is Express.js?

Express.js is a fast, unopinionated, and minimalist web application framework for Node.js. It provides an easy way to create robust web servers and API endpoints by simplifying the complexity involved in handling HTTP requests.

Why use Express.js?

Express offers a variety of benefits that make it popular among developers:

  • Easy setup: Setting up an Express server is quick and straightforward with its intuitive API.
  • Routing: It enables efficient routing of incoming client requests to appropriate handlers.
  • Middleware: You can easily integrate middleware functions into your request-response cycle for enhanced functionality.
  • Extensibility: With its modular design, Express allows developers to add functionality using third-party middleware libraries.
  • Performance: Being lightweight and minimalistic, it ensures excellent performance even under high traffic loads.

Getting started with Express

To get started with building your first app using Express.js:

  1. Install Node.js: If you haven't already installed Node.js on your machine, visit the official website (https://nodejs.org/en/) and follow the installation instructions.

  2. Create a new directory for your project: Open your terminal or command prompt and navigate to the desired location where you want to create your project directory. Use the following command:

   mkdir express-app
Enter fullscreen mode Exit fullscreen mode
  1. Navigate into the project directory:
   cd express-app
Enter fullscreen mode Exit fullscreen mode
  1. Initialize a new npm package within this directory:
    npm init -y 
    ```
{% endraw %}

    This command generates a {% raw %}`package.json`{% endraw %} file that contains information about your project and its dependencies.

5. Install Express.js as a dependency:
{% raw %}

   ```shell
   npm install express
Enter fullscreen mode Exit fullscreen mode
  1. Create a new JavaScript file, such as index.js, and open it in your preferred text editor.

  2. Require the Express module in your file:

    const express = require('express');
    const app = express();
    ```



## Basic Routing with Express

Routing refers to determining how an application responds to client requests for different URLs or routes. In Express, routing is accomplished using methods on the application object (`app`).

To create basic routes, follow these steps:

1. Define a route handler at the root path ("/") that sends "Hello, world!" as the response when accessed.


   ```javascript
    app.get('/', (req, res) => {
        res.send("Hello, world!");
     });
     ```



2. Add additional route handlers for different URLs:

- For example, if you want to handle requests made to "/about" with a custom message:


```javascript
app.get('/about', (req,res)=>{
  res.send("Welcome to the About page!");
});
Enter fullscreen mode Exit fullscreen mode
  • You can define multiple different route handlers based on your application's needs.
  1. Start listening for incoming HTTP requests by adding this line at the end of your script:
      app.listen(3000);
      ```



Now you can run your server by executing `node index.js`. Your web application will be accessible at http://localhost:3000/, where you'll see "Hello, world!" displayed in your browser.

Congratulations! 🎉 You've successfully set up basic routing with Express.js!

This guide covered only the tip of the iceberg when it comes to utilizing Express.js features. Explore its extensive documentation (https://expressjs.com/) to discover more possibilities.

Remember, with Express.js, you have the power to build efficient and scalable web applications. Happy coding!
Enter fullscreen mode Exit fullscreen mode

Top comments (0)