-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
149 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
section | ||
h3(id='app.use') app.use([path], function) | ||
|
||
p. | ||
Use the given middleware <code>function</code>, with optional mount <code>path</code>, | ||
defaulting to "/". | ||
|
||
+js. | ||
var express = require('express'); | ||
var app = express(); | ||
|
||
// simple logger | ||
app.use(function(req, res, next){ | ||
console.log('%s %s', req.method, req.url); | ||
next(); | ||
}); | ||
|
||
// respond | ||
app.use(function(req, res, next){ | ||
res.send('Hello World'); | ||
}); | ||
|
||
app.listen(3000); | ||
|
||
p. | ||
The "mount" path is stripped and is <strong>not</strong> visible | ||
to the middleware <code>function</code>. The main effect of this feature is that | ||
mounted middleware may operate without code changes regardless of its "prefix" | ||
pathname. | ||
|
||
p. | ||
Here's a concrete example, take the typical use-case of serving files in ./public | ||
using the <code>express.static()</code> middleware: | ||
|
||
+js. | ||
// GET /javascripts/jquery.js | ||
// GET /style.css | ||
// GET /favicon.ico | ||
app.use(express.static(__dirname + '/public')); | ||
|
||
p. | ||
Say for example you wanted to prefix all static files with "/static", you could | ||
use the "mounting" feature to support this. Mounted middleware functions are <strong>not</strong> | ||
invoked unless the <code>req.url</code> contains this prefix, at which point | ||
it is stripped when the function is invoked. This affects this function only, | ||
subsequent middleware will see <code>req.url</code> with "/static" included | ||
unless they are mounted as well. | ||
|
||
+js. | ||
// GET /static/javascripts/jquery.js | ||
// GET /static/style.css | ||
// GET /static/favicon.ico | ||
app.use('/static', express.static(__dirname + '/public')); | ||
|
||
p. | ||
The order of which middleware are "defined" using <code>app.use()</code> is | ||
very important, they are invoked sequentially, thus this defines middleware | ||
precedence. For example usually <code>express.logger()</code> is the very | ||
first middleware you would use, logging every request: | ||
|
||
+js. | ||
app.use(express.logger()); | ||
app.use(express.static(__dirname + '/public')); | ||
app.use(function(req, res){ | ||
res.send('Hello'); | ||
}); | ||
|
||
p. | ||
Now suppose you wanted ignore logging requests for static files, but to | ||
continue logging routes and middleware defined after <code>logger()</code>, | ||
you would simply move <code>static()</code> above: | ||
|
||
+js. | ||
app.use(express.static(__dirname + '/public')); | ||
app.use(express.logger()); | ||
app.use(function(req, res){ | ||
res.send('Hello'); | ||
}); | ||
|
||
p. | ||
Another concrete example would be serving files from multiple directories, | ||
giving precedence to "./public" over the others: | ||
|
||
+js. | ||
app.use(express.static(__dirname + '/public')); | ||
app.use(express.static(__dirname + '/files')); | ||
app.use(express.static(__dirname + '/uploads')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters