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, 2022 in Node-js by Vaani
• 7,070 points
1,008 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, 2022 by Neha
• 9,020 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, 2022 in Node-js by Neha
• 9,020 points
532 views
0 votes
0 answers
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,840 points
10,094 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,840 points
14,464 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
1,922 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,451 views
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,020 points
3,107 views
0 votes
1 answer

What is the role of Nodejs and Express in a MERN stack web application when GraphQL is also used?

Node.js is a JavaScript runtime environment, which ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
2,637 views
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, 2022 in Node-js by Neha
• 9,020 points
1,943 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP