【Express】路由
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.' );
});
中间件
var app = require('express')(); app.use(function(req, res, next){
console.log('\n\nALLWAYS');
next();
}); app.get('/a', function(req, res){
console.log('/a: 路由终止');
res.send('a');
});
app.get('/a', function(req, res){
console.log('/a: 永远不会调用');
}); app.get('/b', function(req, res, next){
console.log('/b: 路由未终止');
next();
});
app.use(function(req, res, next){
console.log('SOMETIMES');
next();
});
app.get('/b', function(req, res, next){
console.log('/b (part 2): 抛出错误' );
throw new Error('b 失败');
});
app.use('/b', function(err, req, res, next){
console.log('/b 检测到错误并传递');
next(err);
}); app.get('/c', function(err, req){
console.log('/c: 抛出错误');
throw new Error('c 失败');
});
app.use('/c', function(err, req, res, next){
console.log('/c: 检测到错误但不传递');
next();
}); app.use(function(err, req, res, next){
console.log('检测到未处理的错误: ' + err.message);
res.send('500 - 服务器错误');
}); app.use(function(req, res){
console.log('未处理的路由');
res.send('404 - 未找到');
}); app.listen(3000, function(){
console.log('监听端口3000');
});
【Express】路由的更多相关文章
- express路由和中间件
路由 简单来说,express路由就是用来处理一些请求,响应一些url地址. var express = require('express'); var app = express(); app.ge ...
- express路由探析(续)
上一篇分析了express的路由机制,这次主要补充一些没有说到的东西. 之前说到,Router是中间件容器,Route是路由中间件,他们各自维护一个stack数组,里面存放layer,layer是封装 ...
- Node.js Express 路由文件分类
前言 基于上一篇Web Api Controller分类,在MVC中我们通常要按自己的业务来划分Controller层, 好处多多,那么Express框架作为Node.js的一款MVC框架,那么自然也 ...
- nodejs开发 express路由与中间件
路由 通常HTTP URL的格式是这样的: http://host[:port][path] http表示协议. host表示主机. port为端口,可选字段,不提供时默认为80. path指定请求资 ...
- Express 路由
路由 路由是指如何定义应用的端点(URIs)以及如何响应客户端的请求. 路由是由一个 URI.HTTP 请求(GET.POST等)和若干个句柄组成,它的结构如下: app.METHOD(path, [ ...
- (三)、Express 路由、静态文件、
一.路由 路由(Routing)是由一个 URI(或者叫路径)和一个特定的 HTTP 方法(GET.POST 等)组成的,涉及到应用如何响应客户端对某个网站节点的访问. 每一个路由都可以有一个或者多个 ...
- Node.js express路由简单分析
这2天看了一点node+express的路由源码有了一点眉目,总结一下 对于app.get, 首先给出一张类图: 图1 注意每个路由有一个stack,这个stack中存放了Layer. 路由系统内有三 ...
- node环境下express路由,
1.基本路由概念 路由是指确定应用程序如何响应对特定端点的客户端请求,该请求是URI(或路径)和特定HTTP请求方法(GET,POST等). 每个路由都可以有一个或多个处理函数,这些函数在路由匹配时执 ...
- angular2路由与express路由冲突的问题
angular2的路由定义了一个/a,如果走angular的路由没问题,如果直接访问/a就会出现cannot GET /a的错误,原因就是express的路由问题. 所以路由走angular2,那ex ...
- [转]express 路由控制--next
next() express的路由控制有个next()功能,在定义了多个路由的时候,对匹配的url会按顺序执行, 例如,有这样两个路由,第一个路由会对满足“/”的地址,在req中添加一个user的属性 ...
随机推荐
- MYSQL 体系结构图-LRU FREELIST FLUSH LIST
- Android中使用HttpGet和HttpPost访问HTTP资源
需求:用户登录(name:用户名,pwd:密码) (一)HttpGet :doGet()方法//doGet():将参数的键值对附加在url后面来传递 public String getResultFo ...
- VB 生成xml文件 并使用xsd验证
最近客户的一个需要,要求将数据以xml的形式发送. vb 实现代码 Private Function createXML_old(ByVal xmlName As String) As Boolean ...
- 2015 Multi-University Training Contest 5
#include <iostream> //1002 #include<set> #include<stdio.h> using namespace std; co ...
- ACM比赛技巧
一.语言是最重要的基本功 无论侧重于什么方面,只要是通过计算机程序去最终实现的竞赛,语言都是大家要过的第一道关.亚洲赛区的比赛支持的语言包括C/C++与JAVA.笔者首先说说JAVA,众所周知,作 ...
- 学习JAVA第一部分总结
把自己这几天的学习情况记录下来. 第一章,认识JAVA,了解JAVA的运行机制,虚拟机. 第二章,了解java的注释,标识符,关键字.. 第三章,基本的数据类型,byte short int long ...
- 系统spt_values表--生成时间方便left join
时间处理我给你提供一个思路 系统有个spt_values表,可以构造一整个月的日期,然后左连接你统计好的数据,用CTE表构造多次查询 spt_values的超级经典的应用 http://www. ...
- Web弹框类
using System; using System.Text; namespace Core { /// <summary> /// MessageBox 的摘要说明. /// < ...
- 关于异常的疑难解答:System.Runtime.InteropServices.COMException
COMException exception is thrown when an unrecognized HRESULT is returned from a COM method call.&qu ...
- Data Types
原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administ ...