首页 > 其他 > 详细

【Express】路由

时间:2015-01-29 19:10:43      阅读:285      评论:0      收藏:0      [点我收藏+]
var express = require(‘express‘);

var app = express();
app.set(‘port‘, process.env.PORT || 3000);

app.get(‘/‘, function(req, res) {
    res.type(‘text/plain‘);
    res.send(‘Meadowlark Travel‘);
});
app.get(‘/about‘, function(req, res) {
    res.type(‘text/plain‘);
    res.send(‘About Meadowlark Travel‘);
});

/*
* 通配符

我们对定制的404和500页面的处理与对普通页面的处理应有所区别:用的不是app.get,而是app.use。
app.use是Express添加中间件的一种方法。
在Express中,路由和中间件的添加顺序至关重要。
如果我们把404处理器放在所有路由上面,那首页和关于页面就不能用了,访问这些URL得到的都是404。
 */

// Express能根据回调函数中参数的个数区分404和500处理器
// 定制404页面
app.use(function(req, res) {
    res.type(‘text/plain‘);
    res.status(404);
    res.send(‘404 - Not Found‘);
});

//定制500页面
app.use(function(err, req, res, next) {
    console.error(err.stack);
    res.type(‘text/plain‘);
    res.status(500);
    res.send(‘500 - Server Error‘);
});

app.listen(app.get(‘port‘), function(){
    console.log( ‘Express started on http://localhost:‘ +
    app.get(‘port‘) + ‘; press Ctrl-C to terminate.‘ );
});

 

【Express】路由

原文:http://www.cnblogs.com/jzm17173/p/4260402.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!