node 学习笔记1 router路由
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了node 学习笔记1 router路由相关的知识,希望对你有一定的参考价值。
1. var express = require(‘express‘); var app = express(); app.get(‘/‘, function(req, res){ res.send(‘hello world‘); }); app.listen(3000);
一个最为常见的 路由
2. var admin = express(); admin.get(‘/‘, function (req, res) { console.log(admin.mountpath); // [ ‘/adm*n‘, ‘/manager‘ ] res.send(‘Admin Homepage‘); }) var secret = express(); secret.get(‘/‘, function (req, res) { console.log(secret.mountpath); // /secr*t res.send(‘Admin Secret‘); }); admin.use(‘/secr*t‘, secret); // load the ‘secret‘ router on ‘/secr*t‘, on the ‘admin‘ sub app app.use([‘/adm*n‘, ‘/manager‘], admin); // load the ‘admin‘ router on ‘/adm*n‘ and ‘/manager‘, on the parent app
3. app.all(path, callback [, callback ...])
app.all(‘*‘, requireAuthentication, loadUser);
app.all(‘*‘, requireAuthentication)
app.all(‘*‘, loadUser);
app.all(‘/api/*‘, requireAuthentication)
是有用的“全球”逻辑映射为特定路径前缀或任意匹配。例如,如果你把以下所有其他路线的顶部定义,它要求所有航线从那时起需要身份验证,并自动加载一个用户。记住,这些回调函数不需要作为端点:loadUser可以执行一个任务,然后调用next()继续匹配后续路线。
4 app.delete(path, callback [, callback ...])
app.delete(‘/‘, function (req, res) {
res.send(‘DELETE request to homepage‘);
});
HTTP DELETE请求路由到指定的路径与指定的回调函数。有关更多信息,请参见路由指南。你可以提供多个回调函数的行为就像中间件,除了这些回调函数可以调用下一个(“路线”)绕过其余路线回调(s)。您可以使用这种机制对路线先决条件,然后将控制传递给后续的路线如果没有理由继续当前的路线。
5.app.disable(name)
设置布尔名称设置为false,名字是一个应用程序设置属性的表。调用app.set(“foo”、虚假)为一个布尔属性调用app.disable一样(“foo”)。例如:
app.disable(‘trust proxy‘);
app.get(‘trust proxy‘);
// => false
6.app.disabled(name)
Returns true if the Boolean setting name is disabled (false), where name is one of the properties from the app settings table.
返回true,如果布尔设置名字是禁用的(假),名字是一个应用程序设置的属性表
app.disabled(‘trust proxy‘);
// => true
app.enable(‘trust proxy‘);
app.disabled(‘trust proxy‘);
// => false
7.
app.enable(name)
Sets the Boolean setting name to true, where name is one of the properties from the app settings table. Calling app.set(‘foo‘, true) for a Boolean property is the same as calling app.enable(‘foo‘).
app.enable(‘trust proxy‘);
app.get(‘trust proxy‘);
// => true
app.enabled(name)
Returns true if the setting name is enabled (true), where name is one of the properties from the app settings table.
app.enabled(‘trust proxy‘);
// => false
app.enable(‘trust proxy‘);
app.enabled(‘trust proxy‘);
// => true
8.
app.engine(ext, callback)
Registers the given template engine callback as ext.
By default, Express will require() the engine based on the file extension. For example, if you try to render a “foo.jade” file, Express invokes the following internally, and caches the require() on subsequent calls to increase performance.
app.engine(‘jade‘, require(‘jade‘).__express);
Use this method for engines that do not provide .__express out of the box, or if you wish to “map” a different extension to the template engine.
For example, to map 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 don’t need to do anything.
Some template engines do not follow this convention. The consolidate.js library maps Node template engines to follow this convention, so they work seemlessly with Express.
var engines = require(‘consolidate‘);
app.engine(‘haml‘, engines.haml);
app.engine(‘html‘, engines.hogan);
9.
app.get(name)
Returns the value of name app setting, where name is one of strings in the app settings table. For example:
app.get(‘title‘);
// => undefined
app.set(‘title‘, ‘My Site‘);
app.get(‘title‘);
// => "My Site"
app.get(path, callback [, callback ...])
Routes HTTP GET requests to the specified path with the specified callback functions. For more information, see the routing guide.
You can provide multiple callback functions that behave just like middleware, except these callbacks can invoke next(‘route‘) to bypass the remaining route callback(s). You can use this mechanism to impose pre-conditions on a route, then pass control to subsequent routes if there’s no reason to proceed with the current route.
app.get(‘/‘, function (req, res) {
res.send(‘GET request to homepage‘);
});
10.
以上是关于node 学习笔记1 router路由的主要内容,如果未能解决你的问题,请参考以下文章