一、路由
路由是指应用程序的端点(URI)如何响应客户端请求。
可使用express app对象的方法定义路由。
/*创建express应用程序,该express()函数是express模块导出的顶级函数*/
var express=require('express');
var app=express();
/*该app对象通常表示express应用程序*/
app.get('/',function(req,res){
res.send('hello world');
});
app.listen(3000);
/*
app的方法
1)路由http请求;例如,app.METHOD和app.param
2)配置中间件;app.route
3)渲染HTML试图;app.render
4)注册模板引擎;app.engine
*/
属性
1.app.locals
app.locals的属性是应用程序中的局部变量
res.app响应对象
req.app请求对象
app.locals.title
//=>'My App'
app.locals.emails
//=>'me@myapp.com'
设置后,app.locals属性值将在应用程序的整个生命周期中保持不变,而res.locals属性仅在请求的生命周期内有效。可在应用程序中呈现的模块中访问局部变量。中间件可使用局部变量req.app.locals
app.locals.title='My App';
app.locals.strftime=require('strftime');
app.locals.email='me@myapp.com';
2.app.mountpath
app.mountpath属性包含一个或多个安装了子应用程序的路径模式
var express=require('express');
var app=express();//the main app
var admin=express();//the sub app
admin.get('/',function(req,res){
console.log(admin.mountpath);// /admin
res.send('Admin Homepage');
});
app.use('/admin',admin); //mount the sub app
如果子应用程序安装在多个路径模式上,则app.mountpath返回其安装的模式列表
var admin=express();
admin.get('/',function(req,res){
console.log(admin.mountpath);//['/adm*n','/manager']
res.send('Admin Homepage');
});
var secret=express();
secrect.get('/',function(req,res){
console.log(secrect.mountpath);// /secr*t
res.send('Admin Secret');
});
admin.use('/secr*t',secrect);//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
活动
app.on('mount',callback(parent))
mount当子应用程序挂载在父应用程序上时,会触发该事件。父应用程序将传递给回调函数。
子应用程序将不继承具有默认值的设置的值,因此必须在子应用程序中设置该值。另外,子应用程序可继承没有默认的设置值。
var admin=express();
admin.on('mount',function(parent){
console.log('Admin Mounted');
console.log(parent);//refers to the parent app
});
admin.get('/',function(req,res){
res.send('Admin Homepage');
});
app.use('/admin',admin);
方法
1.app.all(path,callback[,callback..])
1)path:
调用中间件函数的路径,可以有以下几种形式:表示路径的字符串,路径模式,用于匹配路径的正则表达式模式,任何上述组合的阵列。默认根路径'/'
2)callback
回调函数,可以有以下几种形式:中间件功能,一系列中间件函数(用逗号分隔),一系列中间件功能,以上所有的组合。可以调用next('route')以绕过剩余的路由回调
app.all('/secret',function(req,res,next){
console.log('Accessing the secret section ...')
next()//pass control to the next handler
});
app.all('*',requireAuthentication,loadUser)
/*等效*/
app.all('*',requireAuthentication)
app.all('*',loadUser)
/*
'*'全局
requireAuthentication身份验证
loadUser执行任务
*/
/*白名单的全球功能,仅限制以'/api'开头的路径*/
app.all('/api/*',requireAuthentication)
2.app.delete(path,callback[,callback...])
使用指定的回调函数将HTTP DELETE请求路由到指定的路径
app.delete('/',function(req,res){
res.send('DELETE request to homepage');
});
3.app.disable
将布尔值设置name设置为false,其中name是应用程序设置表中的一个属性。调用app.set('foo',false)Boolean属性与调用相同app.disable('foo')。
app.disable('trust proxy');
app.get('trust proxy');//=>false
4.app.disabled
true如果布尔设置name被禁用(false),name则返回,其中是应用程序设置表中的一个属性
app.disabled('trust proxy');//=>true
app.enable('trust proxy');
app.disabled('trust proxy');//=>false
5.app.enable
true如果设置name已启用true,则返回。其中name是应用程序设置表中的一个属性
app.enable('trust proxy');
app.get('trust proxy');//=>true
6.app.enabled
true如果设置name以启用(true),则返回
app.enabled('trust proxy');//=>false
app.enable('trust proxy');
app.enabled('trust proxy');//=>true
7.app.engine(分机,回拨)
将给定的模板引擎注册callback为ext
express会require()根据文件扩展名引擎。
/*第一种:express会在内部调用以下内容,并缓存require()后续调用以提高性能*/
app.engine('pug',require('pug').__express);
/*第二种:未提供.__express开箱即用的引擎,或者希望将不同的扩展‘映射’到模板引擎*/
app.engine('html',require('ejs').renderFile);
/*第三种:*/
var engines=require('consolidate');
app.engine('haml',engines.haml);
app.engine('html',engines.hogan);
8.app.get(名称)
返回name app setting的值,其中name是app settings表中的一个字符串
app.get('title');//=>undefined
app.set('title','My Site');
app.get('title');//=>'My Site'
9.app.get(path,callback[,callback...])
使用指定的回调函数将HTTP GET 请求路由到指定的路径
app.get('/',function(req,res){
res.send('GET request to homepage');
});
10.app.listen(path,[acllback])
启动UNIX套接字并侦听给路径上的连接
/*此方法与node的http.Server.listen()*/
var express=require('express');
var app=express();
app.listen('/tmp/sock');
11.app.listen([port[,host[,backlog]]][,callback])
绑定并侦听指定主机和端口上的连接
/*如果port被省略或为0,操作系统将分配一个任意未使用的端口*/
var express=require('express');
var app=express();
app.listen(1000)
应用程序不会从以下代码继承
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);
/*法二*/
app.listen=function(){
var server=http.createServer(this);
return server.listen.apply(server,arguments);
};