Written by Sümeyye Sever (notes I took while creating web development projects)


Express is a fast, minimal, and flexible web framework for Node.js. It simplifies the process of building web applications and APIs by providing robust features for routing, middleware, and HTTP utility methods.

Why Use Express?

  1. Simplicity: It abstracts the complexities of the native Node.js HTTP module, making it easier to handle requests and responses.
  2. Routing: Provides a straightforward way to define routes for your application, such as GET, POST, PUT, and DELETE requests.
  3. Middleware Support: Allows you to handle requests and responses through middleware, which can modify, validate, or log requests before sending a response.
  4. Scalability: Suitable for building scalable and high-performance applications.
  5. Community Support: Has a large community with many third-party libraries and plugins to extend functionality.
  6. Flexibility: It doesn’t enforce any particular folder structure or code organization, giving you the freedom to design your application as needed.

Creating an Express Server

  1. Create directory

    mkdir “Express Server”

    cd Express\ Server/

  2. Create index.js file

    touch index.js

  3. Initialise NPM

    npm init -y

  4. Install the Express package

    npm i express

  5. Write server application in index.js

    import express from "express"
    
    const app = express();
    
    app.listen(3000, ()=>{
        console.log("Server is running on port 3000");
    })
    
  6. Start server

    node index.js

After this when you go to the localhost:3000 on your browser you will see a page that says Cannot GET / Why? Because when you go to that page you(browser) making a HTTP GET request to your express server. But in your server there is no method that handle the GET request. So you see an error page. So how we can handle this? With routing. But first what is http request and also what is localhost? Click the links below if you want to learn about these topics also.

What is localhost?