9951 explained code solutions for 126 technologies


expressjsHow do I use Express.js to handle asynchronous requests?


Express.js is a web application framework for Node.js that is used to build web applications and APIs. It can be used to handle asynchronous requests by using the async and await keywords.

For example, the following code block uses Express.js to handle an asynchronous request:

const express = require("express");

const app = express();

app.get("/", async (req, res) => {
  const result = await doSomethingAsync();
  res.send(result);
});

app.listen(3000);

In this example, the async keyword is used to indicate that the function passed to app.get is asynchronous. The await keyword is then used to wait for the result of the doSomethingAsync function before sending it back as the response.

Code explanation

  • const express = require("express"); - This line imports the Express.js module.
  • const app = express(); - This line creates an Express.js application instance.
  • app.get("/", async (req, res) => {...}); - This line sets up a route handler for the GET request at the root path. The async keyword indicates that the function passed to app.get is asynchronous.
  • const result = await doSomethingAsync(); - This line waits for the result of the doSomethingAsync function and stores it in the result variable.
  • res.send(result); - This line sends the result back as the response.
  • app.listen(3000); - This line starts the Express.js server on port 3000.

Helpful links

Edit this code on GitHub