Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express app.use

I have been reading documents/urls and really not understand about app.use and its usage. I understand that it is part of connect but I am really not getting that.

Example:

// ignore GET /favicon.ico
app.use(express.favicon());
// add req.session cookie support
app.use(express.cookieSession());
// do something with the session
app.use(count);

can you please explain me all these 3 . what they mean? does this mean based on (1) that app.use is noting but => app.get? app.use(count) what and when is this count be executed (or) called/

Looks Basic but did not get the answers

// ignore GET /favicon.ico
app.use(express.favicon());

// pass a secret to cookieParser() for signed cookies 
app.use(express.cookieParser('manny is cool'));

// add req.session cookie support
app.use(express.cookieSession());

// do something with the session
app.use(count);

// custom middleware
function count(req, res) {
like image 356
The Learner Avatar asked Dec 23 '12 07:12

The Learner


People also ask

What is Express app use?

App. use() is used to bind *application-level middleware to an instance of the app object which is instantiated on the creation of the Express server (router. use() for router-level middleware).

What is Express middleware used for?

js is a routing and Middleware framework for handling the different routing of the webpage and it works between the request and response cycle. Middleware gets executed after the server receives the request and before the controller actions send the response.

Is Express used for frontend or backend?

Express. js is the most popular backend framework for Node. js, and it is an extensive part of the JavaScript ecosystem. It is designed to build single-page, multi-page, and hybrid web applications, it has also become the standard for developing backend applications with Node.

Is Express used for routing?

Use the express.Router class to create modular, mountable route handlers. A Router instance is a complete middleware and routing system; for this reason, it is often referred to as a “mini-app”.


1 Answers

When you call app.use(), you pass in a function to handle requests. As requests come in, Express goes through all of the functions in order until the request is handled.

express.favicon is a simple function that returns favicon.ico when it is requested. It's actually a great example for how to get started with this pattern. You can view the source code by looking at its source: node_modules/express/node_modules/connect/lib/middleware/favicon.js

express.cookieSession is some more middleware for supporting session data, keyed from the client by a cookie.

I don't know what count does... is that your own code? In any case, let me know if this is not clear.

like image 191
Brad Avatar answered Sep 18 '22 11:09

Brad