The `app.use()` Function in Express

The `app.use()` Function in Express

Express apps have a `use()` function that adds a new middleware to the middleware stack. Here's what you need to know.

Express apps have a use() function. This function adds a new middleware to the app.

For example, suppose you want to print the HTTP method (get, post, etc.) and the URL of every request. Here's how you can add a new middleware that prints the HTTP method and URL of every request:

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

app.use((req, res, next) => {
  // For example, a GET request to `/test` will print "GET /test"
  console.log(`${req.method} ${req.url}`);

  next();
});

app.get('/test', (req, res, next) => {
  res.send('ok');
});

// Test the above app using Axios
const server = await app.listen(3000);

const axios = require('axios');
// Prints "get /test"
const res = await axios.get('http://localhost:3000/test');

The Middleware Stack

In Express, everything is middleware. Internally, an Express app has a middleware stack, and calling use() adds a new layer to the stack. Functions that define route handlers, like get() and post() also add layers to the stack. Express executes the middleware stack in order, so the order in which you call use() matters.

For example, one of the most common middleware functions is the cors middleware, which attaches CORS headers to your Express HTTP responses. Make sure you call app.use(cors()) before defining any route handlers or anything else that sends an HTTP response, otherwise you won't get CORS headers!

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

// This response will **NOT** have CORS headers, because order matters.
// Express will run the CORS middleware _after_ this route handler.
app.get('/nocors', (req, res) => {
  res.send('ok');
});

app.use(require('cors')());

// This response will have CORS headers, because this route handler
// is after the CORS middleware in the middleware list.
app.get('/cors', (req, res) => {
  res.send('ok');
});

express node javascript web-development developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Node.js Express PDF to HTML Files Converter Web Application Online in Javascript 2020

Node.js Express PDF to HTML Files Converter Web Application Online in Javascript 2020

Hire Node.JS Developers | Skenix Infotech

We are providing robust Node.JS Development Services with expert Node.js Developers. Get affordable Node.JS Web Development services from Skenix Infotech.

Node.js Express Text Repeater Generator Online Tool in Javascript Full Tutorial 2020

Node.js Express Text Repeater Generator Online Tool in Javascript Full Tutorial 2020

Node.js Express Youtube Video URL Timestamp Link Generator in JavaScript Full Project 2020

Node.js Express Youtube Video URL Timestamp Link Generator in Javascript Full Project 2020

Node.js Express Minify JSON Online Converter Full Web App Deployed to Live Website 2020

Node.js Express Minify JSON Online Converter Full Web App Deployed to Live Website 2020