NodeJS Express what is app use

0 votes

In the docs for the NodeJS express module, the example code has app.use(...).

What is the use function and where is it defined?

Jun 17 in Node-js by Vaani
• 7,020 points
11 views

1 answer to this question.

0 votes

The app object is instantiated on creation of the Express server. It has a middleware stack that can be customized in app.configure()(this is now deprecated in version 4.x).

To setup your middleware, you can invoke app.use(<specific_middleware_layer_here>) for every middleware layer that you want to add (it can be generic to all paths, or triggered only on specific path(s) your server handles), and it will add onto your Express middleware stack. Middleware layers can be added one by one in multiple invocations of use, or even all at once in series with one invocation. See use documentation for more details.

To give an example for conceptual understanding of Express Middleware, here is what my app middleware stack (app.stack) looks like when logging my app object to the console as JSON:

stack: 
   [ { route: '', handle: [Function] },
     { route: '', handle: [Function: static] },
     { route: '', handle: [Function: bodyParser] },
     { route: '', handle: [Function: cookieParser] },
     { route: '', handle: [Function: session] },
     { route: '', handle: [Function: methodOverride] },
     { route: '', handle: [Function] },
     { route: '', handle: [Function] } ]

As you can see, I used app.use(express.bodyParser()), app.use(express.cookieParser()), and other methods to add these express middleware 'layers' to the middleware stack. The routes are blank because I stated that those middleware layers should be triggered on any route when I added them. In the stack printed above, if I built a custom middleware layer that only triggered on the path /user/:id, it would be reflected as a string in the route field of that middleware layer object.

Each layer effectively adds a function to your middleware flow that particularly handles something.

For example, by including bodyParser, you're ensuring that your server uses the express middleware to handle incoming requests. So, because you called app.use, your middleware now parses the body of incoming requests as part of the method for handling incoming requests app.use(bodyParser).

answered Jun 17 by Neha
• 8,560 points

Related Questions In Node-js

0 votes
1 answer

NodeJS express get request is not working on mobile

For your API call, try to add ...READ MORE

answered Jun 9 in Node-js by Neha
• 8,560 points
7 views
0 votes
1 answer

How do I use HTML as the view engine in Express?

Hello @kartik, To make the render engine accept ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,720 points
5,031 views
0 votes
1 answer

What is the best way to run npm install for nested folders?

Hello @kartik, If you want to run a ...READ MORE

answered Jul 17, 2020 in Node-js by Niroj
• 82,720 points
7,206 views
0 votes
1 answer

How to run app.js in nodejs?

Hello @kartik, Assuming I have node and npm properly installed on the ...READ MORE

answered Oct 13, 2020 in Node-js by Niroj
• 82,720 points
1,168 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
967 views
0 votes
1 answer
0 votes
1 answer

Unable to start express server on AWS instance

It's not your code — you can't connect ...READ MORE

answered Oct 1, 2018 in AWS by Priyaj
• 58,080 points
1,995 views
0 votes
1 answer
0 votes
1 answer

NodeJS express get request is not working on mobile

For your API call, try to add ...READ MORE

answered Jun 7 in Node-js by Neha
• 8,560 points
22 views
webinar REGISTER FOR FREE WEBINAR X
Send OTP
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP