nodejs的大名好多人应该是听过的,而作为nodejs web 开发的框架express 大家也应该比较熟悉。

记录一下关于express API 的文档:

express()

创建express 应用.

var express = require('express');
var app = express();app.get('/', function(req, res){res.send('hello world');
});app.listen(3000);

应用

app.set(name, value)

指定对应名称的值

app.set('title', 'My Site');
app.get('title');
// => "My Site"

app.get(name)

获取对应名称的值

app.get('title');
// => undefinedapp.set('title', 'My Site');
app.get('title');
// => "My Site"

app.enable(name)

进行布尔类型的设置

app.enable('trust proxy');
app.get('trust proxy');
// => true

app.disable(name)

同上

app.disable('trust proxy');
app.get('trust proxy');
// => false

app.enabled(name)

检查设置是否可用

app.enabled('trust proxy');
// => falseapp.enable('trust proxy');
app.enabled('trust proxy');
// => true

app.disabled(name)

检查设置是否不可用

app.disabled('trust proxy');
// => trueapp.enable('trust proxy');
app.disabled('trust proxy');
// => false

app.configure([env], callback)

Conditionally invoke callback when env matches app.get('env'), akaprocess.env.NODE_ENV. This method remains for legacy reason, and is effectively an if statement as illustrated in the following snippets. These functions arenot required in order to use app.set() and other configuration methods.

// all environments
app.configure(function(){app.set('title', 'My Application');
})// development only
app.configure('development', function(){app.set('db uri', 'localhost/dev');
})// production only
app.configure('production', function(){app.set('db uri', 'n.n.n.n/prod');
})

effectively sugar for:

// all environments
app.set('title', 'My Application');// development only
if ('development' == app.get('env')) {app.set('db uri', 'localhost/dev');
}// production only
if ('production' == app.get('env')) {app.set('db uri', 'n.n.n.n/prod');
}

app.use([path], function)

Use the given middleware function, with optional mount path, defaulting to "/".

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);

The "mount" path is stripped and is not visible to the middleware function. The main effect of this feature is that mounted middleware may operate without code changes regardless of its "prefix" pathname.

Here's a concrete example, take the typical use-case of serving files in ./public using the express.static() middleware:

// GET /javascripts/jquery.js
// GET /style.css
// GET /favicon.ico
app.use(express.static(__dirname + '/public'));

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 notinvoked unless the req.url contains this prefix, at which point it is stripped when the function is invoked. This affects this function only, subsequent middleware will see req.url with "/static" included unless they are mounted as well.

// GET /static/javascripts/jquery.js
// GET /static/style.css
// GET /static/favicon.ico
app.use('/static', express.static(__dirname + '/public'));

The order of which middleware are "defined" using app.use() is very important, they are invoked sequentially, thus this defines middleware precedence. For example usually express.logger() is the very first middleware 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 to ignore logging requests for static files, but to continue logging routes and middleware defined after logger(), you would simply move 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 from multiple directories, giving precedence to "./public" over the others:

app.use(express.static(__dirname + '/public'));
app.use(express.static(__dirname + '/files'));
app.use(express.static(__dirname + '/uploads'));

settings

The following settings are provided to alter how Express will behave:

  • env Environment mode, defaults to process.env.NODE_ENV or "development"
  • trust proxy Enables reverse proxy support, disabled by default
  • jsonp callback name Changes the default callback name of ?callback=
  • json replacer JSON replacer callback, null by default
  • json spaces JSON response spaces for formatting, defaults to 2 in development, 0 in production
  • case sensitive routing Enable case sensitivity, disabled by default, treating "/Foo" and "/foo" as the same
  • strict routing Enable strict routing, by default "/foo" and "/foo/" are treated the same by the router
  • view cache Enables view template compilation caching, enabled in production by default
  • view engine The default engine extension to use when omitted
  • views The view directory path, defaulting to "process.cwd() + '/views'"

app.engine(ext, callback)

Register the given template engine callback as ext By default will require()the engine based on the file extension. For example if you try to render a "foo.jade" file Express will invoke the following internally, and cache therequire() on subsequent calls to increase performance.

app.engine('jade', require('jade').__express);

For engines that do not provide .__express out of the box - or if you wish to "map" a different extension to the template engine you may use this method. For example mapping the EJS template engine to ".html" files:

app.engine('html', require('ejs').renderFile);

In this case EJS provides a .renderFile() method with the same signature that Express expects: (path, options, callback), though note that it aliases this method as ejs.__express internally so if you're using ".ejs" extensions you dont need to do anything.

Some template engines do not follow this convention, the consolidate.js library was created to map all of node's popular template engines to follow this convention, thus allowing them to work seemlessly within Express.

var engines = require('consolidate');
app.engine('haml', engines.haml);
app.engine('html', engines.hogan);

app.param([name], callback)

Map logic to route parameters. For example when :user is present in a route path you may map user loading logic to automatically provide req.user to the route, or perform validations on the parameter input.

The following snippet illustrates how the callback is much like middleware, thus supporting async operations, however providing the additional value of the parameter, here named as id. An attempt to load the user is then performed, assigning req.user, otherwise passing an error to next(err).

app.param('user', function(req, res, next, id){User.find(id, function(err, user){if (err) {next(err);} else if (user) {req.user = user;next();} else {next(new Error('failed to load user'));}});
});

Alternatively you may pass only a callback, in which case you have the opportunity to alter the app.param() API. For example the express-paramsdefines the following callback which allows you to restrict parameters to a given regular expression.

This example is a bit more advanced, checking if the second argument is a regular expression, returning the callback which acts much like the "user" param example.

app.param(function(name, fn){if (fn instanceof RegExp) {return function(req, res, next, val){var captures;if (captures = fn.exec(String(val))) {req.params[name] = captures;next();} else {next('route');}}}
});

The method could now be used to effectively validate parameters, or also parse them to provide capture groups:

app.param('id', /^\d+$/);app.get('/user/:id', function(req, res){res.send('user ' + req.params.id);
});app.param('range', /^(\w+)\.\.(\w+)?$/);app.get('/range/:range', function(req, res){var range = req.params.range;res.send('from ' + range[1] + ' to ' + range[2]);
});

app.VERB(path, [callback...], callback)

The app.VERB() methods provide the routing functionality in Express, whereVERB is one of the HTTP verbs, such as app.post(). Multiple callbacks may be given, all are treated equally, and behave just like middleware, with the one exception that these callbacks may invoke next('route') to bypass the remaining route callback(s). This mechanism can be used to perform pre-conditions on a route then pass control to subsequent routes when there is no reason to proceed with the route matched.

The following snippet illustrates the most simple route definition possible. Express translates the path strings to regular expressions, used internally to match incoming requests. Query strings are not considered when peforming these matches, for example "GET /" would match the following route, as would "GET /?name=tobi".

app.get('/', function(req, res){res.send('hello world');
});

Regular expressions may also be used, and can be useful if you have very specific restraints, for example the following would match "GET /commits/71dbb9c" as well as "GET /commits/71dbb9c..4c084f9".

app.get(/^\/commits\/(\w+)(?:\.\.(\w+))?$/, function(req, res){var from = req.params[0];var to = req.params[1] || 'HEAD';res.send('commit range ' + from + '..' + to);
});

Several callbacks may also be passed, useful for re-using middleware that load resources, perform validations, etc.

app.get('/user/:id', user.load, function(){// ...
})

These callbacks may be passed within arrays as well, these arrays are simply flattened when passed:

var middleware = [loadForum, loadThread];app.get('/forum/:fid/thread/:tid', middleware, function(){// ...
})app.post('/forum/:fid/thread/:tid', middleware, function(){// ...
})

app.all(path, [callback...], callback)

This method functions just like the app.VERB() methods, however it matches all HTTP verbs.

This method is extremely useful for mapping "global" logic for specific path prefixes or arbitrary matches. For example if you placed the following route at the top of all other route definitions, it would require that all routes from that point on would require authentication, and automatically load a user. Keep in mind that these callbacks do not have to act as end points, loadUser can perform a task, then next() to continue matching subsequent routes.

app.all('*', requireAuthentication, loadUser);

Or the equivalent:

app.all('*', requireAuthentication)
app.all('*', loadUser);

Another great example of this is white-listed "global" functionality. Here the example is much like before, however only restricting paths prefixed with "/api":

app.all('/api/*', requireAuthentication);

app.locals

Application local variables are provided to all templates rendered within the application. This is useful for providing helper functions to templates, as well as app-level data.

app.locals.title = 'My App';
app.locals.strftime = require('strftime');

The app.locals object is a JavaScript Function, which when invoked with an object will merge properties into itself, providing a simple way to expose existing objects as local variables.

app.locals({title: 'My App',phone: '1-250-858-9990',email: 'me@myapp.com'
});app.locals.title
// => 'My App'app.locals.email
// => 'me@myapp.com'

A consequence of the app.locals Object being ultimately a Javascript Function Object is that you must not reuse existing (native) named properties for your own variable names, such as name, apply, bind, call, arguments, length, constructor.

app.locals({name: 'My App'});app.locals.name
// => return 'app.locals' in place of 'My App' (app.locals is a Function !)
// => if name's variable is used in a template, a ReferenceError will be returned.

The full list of native named properties can be found in many specifications. The JavaScript specification introduced original properties, some of which still recognized by modern engines, and the EcmaScript specification then built on it and normalized the set of properties, adding new ones and removing deprecated ones. Check out properties for Functions and Objects if interested.

By default Express exposes only a single app-level local variable, settings.

app.set('title', 'My App');
// use settings.title in a view

app.render(view, [options], callback)

Render a view with a callback responding with the rendered string. This is the app-level variant of res.render(), and otherwise behaves the same way.

app.render('email', function(err, html){// ...
});app.render('email', { name: 'Tobi' }, function(err, html){// ...
});

app.routes

The app.routes object houses all of the routes defined mapped by the associated HTTP verb. This object may be used for introspection capabilities, for example Express uses this internally not only for routing but to provide defaultOPTIONS behaviour unless app.options() is used. Your application or framework may also remove routes by simply by removing them from this object.

console.log(app.routes){ get: [ { path: '/',method: 'get',callbacks: [Object],keys: [],regexp: /^\/\/?$/i },{ path: '/user/:id',method: 'get',callbacks: [Object],keys: [{ name: 'id', optional: false }],regexp: /^\/user\/(?:([^\/]+?))\/?$/i } ],
delete: [ { path: '/user/:id',method: 'delete',callbacks: [Object],keys: [Object],regexp: /^\/user\/(?:([^\/]+?))\/?$/i } ] }

app.listen()

Bind and listen for connections on the given host and port, this method is identical to node's http.Server#listen().

var express = require('express');
var app = express();
app.listen(3000);

The app returned by express() is in fact a JavaScript Function, designed to be passed to node's http servers as a callback to handle requests. This allows you to provide both HTTP and HTTPS versions of your app with the same codebase easily, as the app does not inherit from these, it is simply a callback:

var express = require('express');
var https = require('https');
var http = require('http');
var app = express();http.createServer(app).listen(80);
https.createServer(options, app).listen(443);

The app.listen() method is simply a convenience method defined as, if you wish to use HTTPS or provide both, use the technique above.

app.listen = function(){var server = http.createServer(this);return server.listen.apply(server, arguments);
};

Request

req.params

This property is an array containing properties mapped to the named route "parameters". For example if you have the route /user/:name, then the "name" property is available to you as req.params.name. This object defaults to {}.

// GET /user/tj
req.params.name
// => "tj"

When a regular expression is used for the route definition, capture groups are provided in the array using req.params[N], where N is the nth capture group. This rule is applied to unnamed wild-card matches with string routes such as `/file/*`:

// GET /file/javascripts/jquery.js
req.params[0]
// => "javascripts/jquery.js"

req.query

This property is an object containing the parsed query-string, defaulting to{}.

// GET /search?q=tobi+ferret
req.query.q
// => "tobi ferret"// GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
req.query.order
// => "desc"req.query.shoe.color
// => "blue"req.query.shoe.type
// => "converse"

req.body

This property is an object containing the parsed request body. This feature is provided by the bodyParser() middleware, though other body parsing middleware may follow this convention as well. This property defaults to {}when bodyParser() is used.

// POST user[name]=tobi&user[email]=tobi@learnboost.com
req.body.user.name
// => "tobi"req.body.user.email
// => "tobi@learnboost.com"// POST { "name": "tobi" }
req.body.name
// => "tobi"

req.files

This property is an object of the files uploaded. This feature is provided by thebodyParser() middleware, though other body parsing middleware may follow this convention as well. This property defaults to {} when bodyParser() is used.

For example if a file field was named "image", and a file was uploaded,req.files.image would contain the following File object:

{ size: 74643,path: '/tmp/8ef9c52abe857867fd0a4e9a819d1876',name: 'edge.png',type: 'image/png',hash: false,lastModifiedDate: Thu Aug 09 2012 20:07:51 GMT-0700 (PDT),_writeStream: { path: '/tmp/8ef9c52abe857867fd0a4e9a819d1876',fd: 13,writable: false,flags: 'w',encoding: 'binary',mode: 438,bytesWritten: 74643,busy: false,_queue: [],_open: [Function],drainable: true },length: [Getter],filename: [Getter],mime: [Getter] }

The bodyParser() middleware utilizes the node-formidable module internally, and accepts the same options. An example of this is the keepExtensionsformidable option, defaulting to false which in this case gives you the filename "/tmp/8ef9c52abe857867fd0a4e9a819d1876" void of the ".png" extension. To enable this, and others you may pass them to bodyParser():

app.use(express.bodyParser({ keepExtensions: true, uploadDir: '/my/files' }));

req.param(name)

Return the value of param name when present.

// ?name=tobi
req.param('name')
// => "tobi"// POST name=tobi
req.param('name')
// => "tobi"// /user/tobi for /user/:name
req.param('name')
// => "tobi"

Lookup is performed in the following order:

  • req.params
  • req.body
  • req.query

Direct access to req.bodyreq.params, and req.query should be favoured for clarity - unless you truly accept input from each object.

req.route

The currently matched Route containing several properties such as the route's original path string, the regexp generated, and so on.

app.get('/user/:id?', function(req, res){console.log(req.route);
});

Example output from the previous snippet:

{ path: '/user/:id?',method: 'get',callbacks: [ [Function] ],keys: [ { name: 'id', optional: true } ],regexp: /^\/user(?:\/([^\/]+?))?\/?$/i,params: [ id: '12' ] }

req.cookies

When the cookieParser() middleware is used this object defaults to {}, otherwise contains the cookies sent by the user-agent.

// Cookie: name=tj
req.cookies.name
// => "tj"

req.signedCookies

When the cookieParser(secret) middleware is used this object defaults to {}, otherwise contains the signed cookies sent by the user-agent, unsigned and ready for use. Signed cookies reside in a different object to show developer intent, otherwise a malicious attack could be placed on `req.cookie` values which are easy to spoof. Note that signing a cookie does not mean it is "hidden" nor encrypted, this simply prevents tampering as the secret used to sign is private.

// Cookie: user=tobi.CP7AWaXDfAKIRfH49dQzKJx7sKzzSoPq7/AcBBRVwlI3
req.user
// => "tobi"

req.get(field)

Get the case-insensitive request header field. The Referrer and Referer fields are interchangeable.

req.get('Content-Type');
// => "text/plain"req.get('content-type');
// => "text/plain"req.get('Something');
// => undefined

Aliased as req.header(field).

req.accepts(types)

Check if the given types are acceptable, returning the best match when true, otherwise undefined - in which case you should respond with 406 "Not Acceptable".

The type value may be a single mime type string such as "application/json", the extension name such as "json", a comma-delimited list or an array. When a list or array is given the best match, if any is returned.

// Accept: text/html
req.accepts('html');
// => "html"// Accept: text/*, application/json
req.accepts('html');
// => "html"
req.accepts('text/html');
// => "text/html"
req.accepts('json, text');
// => "json"
req.accepts('application/json');
// => "application/json"// Accept: text/*, application/json
req.accepts('image/png');
req.accepts('png');
// => undefined// Accept: text/*;q=.5, application/json
req.accepts(['html', 'json']);
req.accepts('html, json');
// => "json"

req.accepted

Return an array of Accepted media types ordered from highest quality to lowest.

[ { value: 'application/json',quality: 1,type: 'application',subtype: 'json' },
{ value: 'text/html',quality: 0.5,type: 'text',subtype: 'html' } ]

req.is(type)

Check if the incoming request contains the "Content-Type" header field, and it matches the give mime type.

// With Content-Type: text/html; charset=utf-8
req.is('html');
req.is('text/html');
req.is('text/*');
// => true// When Content-Type is application/json
req.is('json');
req.is('application/json');
req.is('application/*');
// => truereq.is('html');
// => false

req.ip

Return the remote address, or when "trust proxy" is enabled - the upstream address.

req.ip
// => "127.0.0.1"

req.ips

When "trust proxy" is `true`, parse the "X-Forwarded-For" ip address list and return an array, otherwise an empty array is returned. For example if the value were "client, proxy1, proxy2" you would receive the array ["client", "proxy1", "proxy2"] where "proxy2" is the furthest down-stream.

req.path

Returns the request URL pathname.

// example.com/users?sort=desc
req.path
// => "/users"

req.host

Returns the hostname from the "Host" header field (void of portno).

// Host: "example.com:3000"
req.host
// => "example.com"

req.fresh

Check if the request is fresh - aka Last-Modified and/or the ETag still match, indicating that the resource is "fresh".

req.fresh
// => true

req.stale

Check if the request is stale - aka Last-Modified and/or the ETag do not match, indicating that the resource is "stale".

req.stale
// => true

req.xhr

Check if the request was issued with the "X-Requested-With" header field set to "XMLHttpRequest" (jQuery etc).

req.xhr
// => true

req.protocol

Return the protocol string "http" or "https" when requested with TLS. When the "trust proxy" setting is enabled the "X-Forwarded-Proto" header field will be trusted. If you're running behind a reverse proxy that supplies https for you this may be enabled.

req.protocol
// => "http"

req.secure

Check if a TLS connection is established. This is a short-hand for:

'https' == req.protocol;

req.subdomains

Return subdomains as an array.

// Host: "tobi.ferrets.example.com"
req.subdomains
// => ["ferrets", "tobi"]

req.originalUrl

This property is much like req.url, however it retains the original request url, allowing you to rewrite req.url freely for internal routing purposes. For example the "mounting" feature of app.use() will rewrite req.url to strip the mount point.

// GET /search?q=something
req.originalUrl
// => "/search?q=something"

req.acceptedLanguages

Return an array of Accepted languages ordered from highest quality to lowest.

Accept-Language: en;q=.5, en-us
// => ['en-us', 'en']

req.acceptedCharsets

Return an array of Accepted charsets ordered from highest quality to lowest.

Accept-Charset: iso-8859-5;q=.2, unicode-1-1;q=0.8
// => ['unicode-1-1', 'iso-8859-5']

req.acceptsCharset(charset)

Check if the given charset are acceptable.

req.acceptsLanguage(lang)

Check if the given lang are acceptable.

Response

res.status(code)

Chainable alias of node's res.statusCode=.

res.status(404).sendfile('path/to/404.png');

res.set(field, [value])

Set header field to value, or pass an object to set multiple fields at once.

res.set('Content-Type', 'text/plain');res.set({'Content-Type': 'text/plain','Content-Length': '123','ETag': '12345'
})

Aliased as res.header(field, [value]).

res.get(field)

Get the case-insensitive response header field.

res.get('Content-Type');
// => "text/plain"

res.cookie(name, value, [options])

Set cookie name to value, which may be a string or object converted to JSON. The path option defaults to "/".

res.cookie('name', 'tobi', { domain: '.example.com', path: '/admin', secure: true });
res.cookie('rememberme', '1', { expires: new Date(Date.now() + 900000), httpOnly: true });

The maxAge option is a convenience option for setting "expires" relative to the current time in milliseconds. The following is equivalent to the previous example.

res.cookie('rememberme', '1', { maxAge: 900000, httpOnly: true })

An object may be passed which is then serialized as JSON, which is automatically parsed by the bodyParser() middleware.

res.cookie('cart', { items: [1,2,3] });
res.cookie('cart', { items: [1,2,3] }, { maxAge: 900000 });

Signed cookies are also supported through this method. Simply pass thesigned option. When given res.cookie() will use the secret passed toexpress.cookieParser(secret) to sign the value.

res.cookie('name', 'tobi', { signed: true });

Later you may access this value through the req.signedCookie object.

res.clearCookie(name, [options])

Clear cookie name. The path option defaults to "/".

res.cookie('name', 'tobi', { path: '/admin' });
res.clearCookie('name', { path: '/admin' });

res.redirect([status], url)

Redirect to the given url with optional status code defaulting to 302 "Found".

res.redirect('/foo/bar');
res.redirect('http://example.com');
res.redirect(301, 'http://example.com');
res.redirect('../login');

Express supports a few forms of redirection, first being a fully qualified URI for redirecting to a different site:

res.redirect('http://google.com');

The second form is the pathname-relative redirect, for example if you were onhttp://example.com/admin/post/new, the following redirect to /admin would land you at http://example.com/admin:

res.redirect('/admin');

This next redirect is relative to the mount point of the application. For example if you have a blog application mounted at /blog, ideally it has no knowledge of where it was mounted, so where a redirect of /admin/post/new would simply give you http://example.com/admin/post/new, the following mount-relative redirect would give you http://example.com/blog/admin/post/new:

res.redirect('admin/post/new');

Pathname relative redirects are also possible. If you were onhttp://example.com/admin/post/new, the following redirect would land you athttp//example.com/admin/post:

res.redirect('..');

The final special-case is a back redirect, redirecting back to the Referer (or Referrer), defaulting to / when missing.

res.redirect('back');

res.location

Set the location header.

res.location('/foo/bar');
res.location('foo/bar');
res.location('http://example.com');
res.location('../login');
res.location('back');

You can use the same kind of urls as in res.redirect().

For example, if your application is mounted at /blog, the following would set the location header to /blog/admin:

res.location('admin')

res.charset

Assign the charset. Defaults to "utf-8".

res.charset = 'value';
res.send('some html');
// => Content-Type: text/html; charset=value

res.send([body|status], [body])

Send a response.

res.send(new Buffer('whoop'));
res.send({ some: 'json' });
res.send('some html');
res.send(404, 'Sorry, we cannot find that!');
res.send(500, { error: 'something blew up' });
res.send(200);

This method performs a myriad of useful tasks for simple non-streaming responses such as automatically assigning the Content-Length unless previously defined and providing automatic HEAD and HTTP cache freshness support.

When a Buffer is given the Content-Type is set to "application/octet-stream" unless previously defined as shown below:

res.set('Content-Type', 'text/html');
res.send(new Buffer('some html'));

When a String is given the Content-Type is set defaulted to "text/html":

res.send('some html');

When an Array or Object is given Express will respond with the JSON representation:

res.send({ user: 'tobi' })
res.send([1,2,3])

Finally when a Number is given without any of the previously mentioned bodies, then a response body string is assigned for you. For example 200 will respond will the text "OK", and 404 "Not Found" and so on.

res.send(200)
res.send(404)
res.send(500)

res.json([status|body], [body])

Send a JSON response. This method is identical to res.send() when an object or array is passed, however it may be used for explicit JSON conversion of non-objects (null, undefined, etc), though these are technically not valid JSON.

res.json(null)
res.json({ user: 'tobi' })
res.json(500, { error: 'message' })

res.jsonp([status|body], [body])

Send a JSON response with JSONP support. This method is identical tores.json() however opts-in to JSONP callback support.

res.jsonp(null)
// => nullres.jsonp({ user: 'tobi' })
// => { "user": "tobi" }res.jsonp(500, { error: 'message' })
// => { "error": "message" }

By default the JSONP callback name is simply callback, however you may alter this with the jsonp callback name setting. The following are some examples of JSONP responses using the same code:

// ?callback=foo
res.jsonp({ user: 'tobi' })
// => foo({ "user": "tobi" })app.set('jsonp callback name', 'cb');// ?cb=foo
res.jsonp(500, { error: 'message' })
// => foo({ "error": "message" })

res.type(type)

Sets the Content-Type to the mime lookup of type, or when "/" is present the Content-Type is simply set to this literal value.

res.type('.html');
res.type('html');
res.type('json');
res.type('application/json');
res.type('png');

res.format(object)

Performs content-negotiation on the request Accept header field when present. This method uses req.accepted, an array of acceptable types ordered by their quality values, otherwise the first callback is invoked. When no match is performed the server responds with 406 "Not Acceptable", or invokes thedefault callback.

The Content-Type is set for you when a callback is selected, however you may alter this within the callback using res.set() or res.type() etcetera.

The following example would respond with { "message": "hey" } when the Accept header field is set to "application/json" or "*/json", however if "*/*" is given then "hey" will be the response.

res.format({'text/plain': function(){res.send('hey');},'text/html': function(){res.send('hey');},'application/json': function(){res.send({ message: 'hey' });}
});

In addition to canonicalized MIME types you may also use extnames mapped to these types, providing a slightly less verbose implementation:

res.format({text: function(){res.send('hey');},html: function(){res.send('hey');},json: function(){res.send({ message: 'hey' });}
});

res.attachment([filename])

Sets the Content-Disposition header field to "attachment". If a filename is given then the Content-Type will be automatically set based on the extname viares.type(), and the Content-Disposition's "filename=" parameter will be set.

res.attachment();
// Content-Disposition: attachmentres.attachment('path/to/logo.png');
// Content-Disposition: attachment; filename="logo.png"
// Content-Type: image/png

res.sendfile(path, [options], [fn]])

Transfer the file at the given path.

Automatically defaults the Content-Type response header field based on the filename's extension. The callback fn(err) is invoked when the transfer is complete or when an error occurs.

Options:

  • maxAge in milliseconds defaulting to 0
  • root root directory for relative filenames

This method provides fine-grained support for file serving as illustrated in the following example:

app.get('/user/:uid/photos/:file', function(req, res){var uid = req.params.uid, file = req.params.file;req.user.mayViewFilesFrom(uid, function(yes){if (yes) {res.sendfile('/uploads/' + uid + '/' + file);} else {res.send(403, 'Sorry! you cant see that.');}});
});

res.download(path, [filename], [fn])

Transfer the file at path as an "attachment", typically browsers will prompt the user for download. The Content-Disposition "filename=" parameter, aka the one that will appear in the brower dialog is set to path by default, however you may provide an override filename.

When an error has ocurred or transfer is complete the optional callback fn is invoked. This method uses res.sendfile() to transfer the file.

res.download('/report-12345.pdf');res.download('/report-12345.pdf', 'report.pdf');res.download('/report-12345.pdf', 'report.pdf', function(err){if (err) {// handle error, keep in mind the response may be partially-sent// so check res.headerSent} else {// decrement a download credit etc}
});

res.links(links)

Join the given links to populate the "Link" response header field.

res.links({next: 'http://api.example.com/users?page=2',last: 'http://api.example.com/users?page=5'
});

yields:

Link: <http://api.example.com/users?page=2>; rel="next", <http://api.example.com/users?page=5>; rel="last"

res.locals

Response local variables are scoped to the request, thus only available to the view(s) rendered during that request / response cycle, if any. Otherwise this API is identical to app.locals.

This object is useful for exposing request-level information such as the request pathname, authenticated user, user settings etcetera.

app.use(function(req, res, next){res.locals.user = req.user;res.locals.authenticated = ! req.user.anonymous;next();
});

res.render(view, [locals], callback)

Render a view with a callback responding with the rendered string. When an error occurs next(err) is invoked internally. When a callback is provided both the possible error and rendered string are passed, and no automated response is performed.

res.render('index', function(err, html){// ...
});res.render('user', { name: 'Tobi' }, function(err, html){// ...
});

Middleware

basicAuth()

Basic Authentication middleware, populating req.user with the username.

Simple username and password:

app.use(express.basicAuth('username', 'password'));

Callback verification:

app.use(express.basicAuth(function(user, pass){return 'tj' == user & 'wahoo' == pass;
}));

Async callback verification, accepting fn(err, user), in this case req.user will be the user object passed.

app.use(express.basicAuth(function(user, pass, fn){User.authenticate({ user: user, pass: pass }, fn);
}))

bodyParser()

Request body parsing middleware supporting JSON, urlencoded, and multipart requests. This middleware is simply a wrapper for the json()urlencoded(), and multipart() middleware.

app.use(express.bodyParser());// is equivalent to:
app.use(express.json());
app.use(express.urlencoded());
app.use(express.multipart());

For security sake, it's better to disable file upload if your application doesn't need it. To do this, use only the needed middleware, i.e. don't use thebodyParser and multipart() middleware:

app.use(express.json());
app.use(express.urlencoded());

If your application needs file upload you should set up a strategy for dealing with those files.

compress()

Compress response data with gzip / deflate. This middleware should be placed "high" within the stack to ensure all responses may be compressed.

app.use(express.logger());
app.use(express.compress());
app.use(express.methodOverride());
app.use(express.bodyParser());

cookieParser()

Parses the Cookie header field and populates req.cookies with an object keyed by the cookie names. Optionally you may enabled signed cookie support by passing a secret string.

app.use(express.cookieParser());
app.use(express.cookieParser('some secret'));

cookieSession()

Provides cookie-based sessions, and populates req.session. This middleware takes the following options:

  • key cookie name defaulting to connect.sess
  • secret prevents cookie tampering
  • cookie session cookie settings, defaulting to { path: '/', httpOnly: true, maxAge: null }
  • proxy trust the reverse proxy when setting secure cookies (via "x-forwarded-proto")
app.use(express.cookieSession());

To clear a cookie simply assign the session to null before responding:

req.session = null

csrf()

CSRF protection middleware.

By default this middleware generates a token named "_csrf" which should be added to requests which mutate state, within a hidden form field, query-string etc. This token is validated against req.csrfToken().

The default value function checks req.body generated by the bodyParser()middleware, req.query generated by query(), and the "X-CSRF-Token" header field.

This middleware requires session support, thus should be added somewhere below session().

directory()

Directory serving middleware, serves the given path. This middleware may be paired with static() to serve files, providing a full-featured file browser.

app.use(express.directory('public'))
app.use(express.static('public'))

This middleware accepts the following options:

  • hidden display hidden (dot) files. Defaults to false.
  • icons display icons. Defaults to false.
  • filter Apply this filter function to files. Defaults to false.

nodejs express 学习相关推荐

  1. Nodejs+Express学习二(Mongoose基础了解)

    学习Node注定少不了与数据库打交道,而MongoDB和Node可以说是绝配,这篇主要是简单介绍Mongoose这个模块. 由于本人也是边学边写的这篇文章,绝对会有新手的味道,请大神看到这里就表往下看 ...

  2. 开始nodejs+express的学习+实践(8)

    为什么80%的码农都做不了架构师?>>>    1.session使用 介绍的非常详细: http://www.cnblogs.com/chenchenluo/p/4197181.h ...

  3. Nodejs入门学习,nodejs web开发入门,npm、express、socket配置安装、nodejs聊天室开发

    一.Node.js:服务器端的 JavaScript 运行环境,它具有无阻塞和事件驱动等特色,采用 V8 引擎,实现了类似 Apache 和 Nginx 的 Web 服务,让你可以通过它来搭建基于 J ...

  4. windows下nodejs express安装及入门网站,视频资料,开源项目介绍

    windows下nodejs express安装及入门网站,视频资料,开源项目介绍,pm2,supervisor,npm,Pomelo,Grunt安装使用注意事项等总结 第一步:下载安装文件 下载地址 ...

  5. NodeJS+Express+MongoDB 简单实现数据录入及回显展示【Study笔记】

    近期在看NodeJS相关 不得不说NodeJS+Express 进行网站开发是很不错,对于喜欢玩JS的来说真是很好的一种Web开发组合 在接触NodeJS时受平时Java或者C#中API接口等开发的思 ...

  6. Nodejs+express+vue+Elementui酒店客房管理系统

    酒店客房管理系统,本网站是基于WEB采用了BS架构开发,利用当前最流行的nodejs+vue与mysql数据库相结合的技术,针对某个酒店进行管理,实现了该酒店的现代化的信息管理,从房客到酒店的房间都进 ...

  7. Nodejs+express+vue在线音乐播放器网站

    在线音乐网站是一款多功能的音乐播放网站,本网站适用于各种音乐分享和推广平台,支持在线音乐播放试听,支持个人的音乐收藏,丰富的音乐和图片资源,是整个网站美观多彩,方便管理员的管理和信息维护,界面友好,操 ...

  8. Nodejs+express+vue+Elementui网上电子商城系统附购物车源码

    本电子商城系统的意义在于将线下的交易流程结合电子商务自身的操作和管理特性,通过在系统搭建上进行合理的优化和设计搬到线上.一方面,达到通过电子商城系统实现并扩大商品交易的业务需求:另一方面,可以节省商家 ...

  9. TWaver html5 + NodeJS + express + websocket.io + redis 快速搭建项目(一)

    最近看到TWaver 的html5版本即将发布了,于是今天用TWaver HTML5 + NodeJS + express + websocket.io + redis搭建了一个简单原型.先发出几张效 ...

最新文章

  1. java foreach 删除_为什么java不要在foreach循环里进行元素的remove/add操作
  2. C#下的插件体系探讨
  3. burp导出html,BurpSuite 的导入和导出
  4. OpenCASCADE绘制测试线束:OCAF 命令之应用命令
  5. ASP.Net缓存 1
  6. 跨站脚本攻击(Cross‐Site Scripting (XSS))实践
  7. linux中php配置
  8. RelativeDateFormat时间计算工具类
  9. Android实现支持缩放平移图片
  10. 复数基础—— i = 根号 -1 _3
  11. react打包后图片丢失_手写Webpack从0编译Vue/React项目
  12. Python科学计算初探——余弦相似度
  13. 测试显卡cpu中文软件,显卡信息检测工具(GPUinfo)
  14. Oracle搜索所有表查找关键字,根据关键字查询oracle中所有表的记录
  15. cad插件_CAD插件坐标标注安装教程
  16. QString中去除空格
  17. 每天好心情——Python画一棵樱花树
  18. ppm调制matlab程序,求助:谁能帮我编写一个4ppm的解调程序 谢谢了
  19. rsync同步技巧---跳过指定文件或目录
  20. Nginx下上传图片404

热门文章

  1. iOS 设计模式浅析 0 - 前言
  2. Underscore.js (1.7.0)-集合(Collections)(25)
  3. jQuery EasyUI API 中文文档 - ValidateBox验证框
  4. java 实现Active Object思想
  5. lamp源码三层结构
  6. Facebook对MySQL全表扫描性能的改进
  7. vista iis7上安装php4.4.7
  8. 列表与元组——Python基础语法
  9. 6-3 二叉搜索树中的最近公共祖先 (25 分)
  10. 6.4(反向显示一个整数)