它可用于
app.get('/', function(req, res){
res.send('hello world');
});
这是在接收到定义的端口上的请求时显示在浏览器中。
命令 app.get 还有哪些其他用途?
app.get 有两个用途。
第一种是像您展示的那样将其用作路由,或者甚至在路由之外使用多个中间件,如下例所示:
var middleware = function(req, res, next) {
//do something, then call the next() middleware.
next();
}
app.get('/', middleware, function (req, res) {
res.render('template');
});
但 app.get 也可以与 app.set 一起使用:
var appEnv = app.get('env'); //tells you the environment, development or production
var appPort = app.get('port'); //tells you the port the app runs on
console.log('app is running in ' + appEnv + ' environment and on port: ' + appPort);
app.set('any-string', 'any value'); //set custom app level value
var any_string = app.get('any-string'); //retrieve custom app level value
console.log('any_string = ' + any_string);
这就是我到目前为止发现的 app.get 的用途,
玩得开心
亚沙