nodejs Express - api reference

  • 格式:pdf
  • 大小:478.89 KB
  • 文档页数:40

12-12-5Express - api reference

1/40expressjs.com/api.htmlexpress()Create an express application.var express = require('express');var app = express();app.get('/', function(req, res){ res.send('hello world');});app.listen(3000);Applicationapp.set(name, value)Assigns setting name to value.app.set('title', 'My Site');app.get('title');// => "My Site"app.get(name)12-12-5Express - api reference

2/40expressjs.com/api.htmlGet setting name value.app.get('title');// => undefinedapp.set('title', 'My Site');app.get('title');// => "My Site"app.enable(name)Set setting name to true.app.enable('trust proxy');app.get('trust proxy');// => trueapp.disable(name)Set setting name to false.app.disable('trust proxy');app.get('trust proxy');// => falseapp.enabled(name)Check if setting name is enabled.Applicationexpress()app.set()app.get()app.enable()app.disable()app.enabled()expressHome API Reference Guide Applications Community FAQ12-12-5Express - api reference

3/40expressjs.com/api.htmlapp.enabled('trust proxy');// => falseapp.enable('trust proxy');app.enabled('trust proxy');// => trueapp.disabled(name)Check if setting name is disabled.app.disabled('trust proxy');// => trueapp.enable('trust proxy');app.disabled('trust proxy');// => falseapp.configure([env], callback)Conditionally invoke callback when env matchesapp.get('env'), aka process.env.NODE_ENV. This methodremains for legacy reason, and is effectively an ifstatement as illustrated in the following snippets. Thesefunctions are not required in order to use app.set()and other configuration methods.// all environmentsapp.configure(function(){ app.set('title', 'My Application');})// development onlyapp.configure('development', function(){ app.set('db uri', 'localhost/dev');})app.disabled()app.configure()app.use()app.engine()app.param()application settingsapplication routingapp.all()app.localsapp.render()app.routesapp.listen()RequestResponseMiddleware12-12-5Express - api reference

4/40expressjs.com/api.html// production onlyapp.configure('production', function(){ app.set('db uri', 'n.n.n.n/prod');})effectively sugar for:// all environmentsapp.set('title', 'My Application');// development onlyif ('development' == app.get('env')) { app.set('db uri', 'localhost/dev');}// production onlyif ('production' == app.get('env')) { app.set('db uri', 'n.n.n.n/prod');}app.use([path], function)Use the given middleware function, with optionalmount path, defaulting to "/".var express = require('express');var app = express();// simple loggerapp.use(function(req, res, next){ console.log('%s %s', req.method, req.url); next();});// respondapp.use(function(req, res, next){ res.send('Hello World');});app.listen(3000);Express - api reference

5/40expressjs.com/api.htmlThe "mount" path is stripped and is not visible to themiddleware function. The main effect of this feature isthat mounted middleware may operate without codechanges regardless of its "prefix" pathname.Here's a concrete example, take the typical use-case ofserving files in ./public using the express.static()middleware:// GET /javascripts/jquery.js// GET /style.css// GET /favicon.icoapp.use(express.static(__dirname + '/public'));Say for example you wanted to prefix all static files with"/static", you could use the "mounting" feature tosupport this. Mounted middleware functions are notinvoked unless the req.url contains this prefix, atwhich point it is stripped when the function is invoked.This affects this function only, subsequent middlewarewill see req.url with "/static" included unless they aremounted as well.// GET /static/javascripts/jquery.js// GET /static/style.css// GET /static/favicon.icoapp.use('/static', express.static(__dirname + '/public'));The order of which middleware are "defined" usingapp.use() is very important, they are invokedsequentially, thus this defines middleware precedence.For example usually express.logger() is the very firstmiddleware you would use, logging every request:app.use(express.logger());app.use(express.static(__dirname + '/public'));app.use(function(req, res){ res.send('Hello');});Now suppose you wanted ignore logging requests for12-12-5Express - api reference

6/40expressjs.com/api.htmlstatic files, but to continue logging routes andmiddleware defined after logger(), you would simplymove static() above:app.use(express.static(__dirname + '/public'));app.use(express.logger());app.use(function(req, res){ res.send('Hello');});Another concrete example would be serving files frommultiple directories, giving precedence to "./public" overthe others:app.use(express.static(__dirname + '/public'));app.use(express.static(__dirname + '/files'));app.use(express.static(__dirname + '/uploads'));settingsThe following settings are provided to alter how Expresswill behave:env Environment mode, defaults toprocess.env.NODE_ENV or "development"trust proxy Enables reverse proxy support,disabled by defaultjsonp callback name Changes the default callbackname of ?callback=json replacer JSON replacer callback, null bydefaultjson spaces JSON response spaces for formatting,defaults to 2 in development, 0 in productioncase sensitive routing Enable case sensitivity,disabled by default, treating "/Foo" and "/foo" asthe samestrict routing Enable strict routing, by default"/foo" and "/foo/" are treated the same by the