expressjs
  1. expressjs-handling-http-requests-and-responses

ExpressJs Handling HTTP Requests and Responses

Syntax

app.METHOD(PATH, HANDLER)
  • app: an instance of the express module
  • METHOD: HTTP request method (e.g. GET, POST, DELETE)
  • PATH: a path on the server
  • HANDLER: a function that is executed when the route is matched

Example

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

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

app.post('/user', (req, res) => {
  res.send('Created a new user');
});

app.delete('/user/:id', (req, res) => {
  res.send(`Deleted user with id: ${req.params.id}`);
});

app.listen(3000, () => {
  console.log('Server is listening on port 3000');
});

Output

When you navigate to http://localhost:3000/, you should see the following output:

Hello, world!

When you make a POST request to http://localhost:3000/user, you should see the following output:

Created a new user

When you make a DELETE request to http://localhost:3000/user/123, you should see the following output:

Deleted user with id: 123

Explanation

Express is a popular web framework for Node.js that provides a simple and flexible way to handle HTTP requests and responses. The app.METHOD() function is used to define a route for a specific HTTP method, such as GET, POST, or DELETE. The PATH parameter is a string that defines the route path, and the HANDLER parameter is a function that is executed when the route is matched.

In the example above, we defined three routes:

  • A GET route for the root path /
  • A POST route for the path /user
  • A DELETE route for the path /user/:id, where :id is a route parameter

Each route has a corresponding handler function that sends a response back to the client. In the case of a GET request, we simply send a "Hello, world!" message. In the case of a POST request, we send a message indicating that a new user has been created. And for a DELETE request, we send a message that includes the ID of the user that was deleted.

Use

Express can be used to handle HTTP requests and responses in a wide variety of web applications, including APIs, web services, and full-stack web applications. With a simple and intuitive syntax, Express makes it easy to define routes and handle incoming requests, and its modular architecture allows developers to use only the features they need.

Important Points

  • Express provides a simple and flexible way to handle HTTP requests and responses
  • The app.METHOD() function is used to define a route for a specific HTTP method
  • The PATH parameter is a string that defines the route path
  • The HANDLER parameter is a function that is executed when the route is matched
  • Express can be used in a wide variety of web applications, including APIs and full-stack web applications

Summary

In this tutorial, we learned how to use Express to handle HTTP requests and responses. By defining routes with app.METHOD(), we can respond to incoming HTTP requests with customized messages and actions. Express provides a simple and flexible way to handle incoming requests, and its modular architecture allows developers to build web applications with only the features they need.

Published on: