_includes/api/en/4x/app-use.md
Mounts the specified middleware function or functions
at the specified path:
the middleware function is executed when the base of the requested path matches path.
{% include api/en/4x/routing-args.html %}
A route will match any path that follows its path immediately with a "/".
For example: app.use('/apple', ...) will match "/apple", "/apple/images",
"/apple/images/news", and so on.
Since path defaults to "/", middleware mounted without a path will be executed for every request to the app.
For example, this middleware function will be executed for every request to the app:
app.use(function (req, res, next) {
console.log('Time: %d', Date.now())
next()
})
Sub-apps will:
For details, see Application settings.
</div>Middleware functions are executed sequentially, therefore the order of middleware inclusion is important.
// this middleware will not allow the request to go beyond it
app.use(function (req, res, next) {
res.send('Hello World')
})
// requests will never reach this route
app.get('/', function (req, res) {
res.send('Welcome')
})
Error-handling middleware
Error-handling middleware always takes four arguments. You must provide four arguments to identify it as an error-handling middleware function. Even if you don't need to use the next object, you must specify it to maintain the signature. Otherwise, the next object will be interpreted as regular middleware and will fail to handle errors. For details about error-handling middleware, see: [Error handling](/{{ page.lang }}/guide/error-handling.html).
Define error-handling middleware functions in the same way as other middleware functions, except with four arguments instead of three, specifically with the signature (err, req, res, next)):
app.use(function (err, req, res, next) {
console.error(err.stack)
res.status(500).send('Something broke!')
})
The following table provides some simple examples of valid path values for
mounting middleware.
app.use('/abcd', function (req, res, next) {
next()
})
app.use('/abc?d', function (req, res, next) {
next()
})
This will match paths starting with /abcd, /abbcd, /abbbbbcd, and so on:
app.use('/ab+cd', function (req, res, next) {
next()
})
This will match paths starting with /abcd, /abxcd, /abFOOcd, /abbArcd, and so on:
app.use('/ab*cd', function (req, res, next) {
next()
})
This will match paths starting with /ad and /abcd:
app.use('/a(bc)?d', function (req, res, next) {
next()
})
app.use(/\/abc|\/xyz/, function (req, res, next) {
next()
})
app.use(['/abcd', '/xyza', /\/lmn|\/pqr/], function (req, res, next) {
next()
})
The following table provides some simple examples of middleware functions that
can be used as the callback argument to app.use(), app.METHOD(), and app.all().
app.use(function (req, res, next) {
next()
})
A router is valid middleware.
var router = express.Router()
router.get('/', function (req, res, next) {
next()
})
app.use(router)
An Express app is valid middleware.
var subApp = express()
subApp.get('/', function (req, res, next) {
next()
})
app.use(subApp)
var r1 = express.Router()
r1.get('/', function (req, res, next) {
next()
})
var r2 = express.Router()
r2.get('/', function (req, res, next) {
next()
})
app.use(r1, r2)
var r1 = express.Router()
r1.get('/', function (req, res, next) {
next()
})
var r2 = express.Router()
r2.get('/', function (req, res, next) {
next()
})
app.use([r1, r2])
function mw1 (req, res, next) { next() }
function mw2 (req, res, next) { next() }
var r1 = express.Router()
r1.get('/', function (req, res, next) { next() })
var r2 = express.Router()
r2.get('/', function (req, res, next) { next() })
var subApp = express()
subApp.get('/', function (req, res, next) { next() })
app.use(mw1, [mw2, r1, r2], subApp)
Following are some examples of using the express.static middleware in an Express app.
Serve static content for the app from the "public" directory in the application directory:
// GET /style.css etc
app.use(express.static(path.join(__dirname, 'public')))
Mount the middleware at "/static" to serve static content only when their request path is prefixed with "/static":
// GET /static/style.css etc.
app.use('/static', express.static(path.join(__dirname, 'public')))
Disable logging for static content requests by loading the logger middleware after the static middleware:
app.use(express.static(path.join(__dirname, 'public')))
app.use(logger())
Serve static files from multiple directories, but give precedence to "./public" over the others:
app.use(express.static(path.join(__dirname, 'public')))
app.use(express.static(path.join(__dirname, 'files')))
app.use(express.static(path.join(__dirname, 'uploads')))