可以参考Express官网关于路由一节:http://expressjs.com/guide/routing.html

1:通过使用GET、POST方式定义主页路由,app.js:

var express = require('express');
var app = express(); app.get('/', function(req,res){
res.send('欢迎来到Express首页');
});
app.post('/', function(req,res){
res.send('Welcome to Express Index.');
});

2:路由路径以及参数定义

var express = require('express');
var app = express(); app.get('/users', function(req,res){
res.send('获取用户列表');
});
app.get('/users/:id', function(req,res){
res.send('user id:'+req.params.id);
});

浏览器中输入:http://127.0.0.1:3000/users/,http://127.0.0.1:3000/users/111

3:正则路由,值匹配方式与正则表达式一致,例如:

var express = require('express');
var app = express(); app.get('/ho?l', function(req,res){
res.send('hl,hol');
});
app.get('/ho+l', function(req,res){
res.send('hol,hool,hooool,and so on');
});
app.get('/ho*l', function(req,res){
res.send('hol,holl,hoal,ho1000l,and so on');
});
app.get('/ho(ab)?l', function(req,res){
res.send('hol,habl');
});

4:Route Handler:next()

var express = require('express');
var app = express(); app.get('/a/b', function(req,res,next){
console.log('response will be send by the next function...');
next();
},function(req,res){
res.send('/a/b');
});

注意在next()所在的路由中,不能有输出,要在最后的处理函数中输出,否则后台会输出以下错误内容:

Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (http.js::)
at ServerResponse.header (/home/y/my_note/nodejs/myapp/node_modules/express/lib/response.js::)
at ServerResponse.send (/home/y/my_note/nodejs/myapp/node_modules/express/lib/response.js::)
at fn (/home/y/my_note/nodejs/myapp/node_modules/express/lib/response.js::)
at View.exports.renderFile [as engine] (/home/y/my_note/nodejs/myapp/node_modules/jade/lib/jade.js::)
at View.render (/home/y/my_note/nodejs/myapp/node_modules/express/lib/view.js::)
at Function.app.render (/home/y/my_note/nodejs/myapp/node_modules/express/lib/application.js::)
at ServerResponse.res.render (/home/y/my_note/nodejs/myapp/node_modules/express/lib/response.js::)
at module.exports (/home/y/my_note/nodejs/myapp/app.js::)
at Layer.handle_error (/home/y/my_note/nodejs/myapp/node_modules/express/lib/router/layer.js::)

5:Response Methods

Method Description
res.download() Prompt a file to be downloaded.
res.end() End the response process.
res.json() Send a JSON response.
res.jsonp() Send a JSON response with JSONP support.
res.redirect() Redirect a request.
res.render() Render a view template.
res.send() Send a response of various types.
res.sendFile Send a file as an octet stream.
res.sendStatus() Set the response status code and send its string representation as the response body.

6:app.route()

app.route('/book')
.get(function(req,res){
res.send('get a book');
})
.post(function(req,res){
res.send('add a book');
})
.put(function(req,res){
res.send('update the book');
});

7:Express.Router

app.js

var routes = require('./routes/index');
var users = require('./routes/users');

// view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'jade');


app.use('/', routes);
app.use('/users', users);
index.js
var express = require('express');
var router = express.Router(); /* GET home page. */
router.get('/', function(req, res) {
res.render('index', { title: 'Express!' });
}); module.exports = router;

Express4 Route笔记的更多相关文章

  1. express4.X 笔记

    express是node的web框架,更新频繁,3.X到4.X有了很大的改变.网上的例子,各种版本的都有,为了以后方便,现在重新认真看一遍4.X的API,统一以后的使用方法.在J2EE上落后了,在ex ...

  2. NodeJS学习笔记 - Express4.x路由操作

    一.为Express添加about路由 1.新建js文件,about.js 2.打开about.js,并输入以下代码: var express=require('express'); var rout ...

  3. Asp.net core (学习笔记 路由和语言 route & language)

    https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-2.1 https://doc ...

  4. Route学习笔记之Area的Route注册

    前一段时间接触了MVC的Area可以将模型.控制器和视图分成各个独立的节点.分区之后,区域路由注册的需求就出来了. 默认的 在MVC项目上右键添加区域之后,在文件夹下会自动添加一个FolderName ...

  5. Thinkphp5笔记八:路由别名Route

    主要作用:隐藏自己的真实路由名称 application/Route.php 使用方法一: <?php use think\Route; Route::alias('home','index/i ...

  6. linux学习笔记31--命令route和routetrace

    Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两个不同的子网之间的通信,需要一台连接两个网络的路由器,或 ...

  7. linux命令学习笔记(53):route命令

    Linux系统的route命令用于显示和操作IP路由表(show / manipulate the IP routing table).要实现两 个不同的子网之间的通信,需要一台连接两个网络的路由器, ...

  8. Route学习笔记

    前言 UrlRoutingModule.class:这块的代码关联了上一篇中路由部分的一个详细说明 一:Route的讲解 1. 路由模板匹配 添加路由: MapRoute 剔除的路由:IgnoreRo ...

  9. ASP.NET MVC 学习笔记1 Talk about controller & route

    For the sake of learning programming better, I'd like to increase the frequency of using English. So ...

随机推荐

  1. poj3177 Redundant Paths

    Description In order to get from one of the F (1 <= F <= 5,000) grazing fields (which are numb ...

  2. Android手势操作

    xml文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:to ...

  3. 深入理解linux网络技术内幕读书笔记(九)--中断与网络驱动程序

    Table of Contents 1 接收到帧时通知驱动程序 1.1 轮询 1.2 中断 2 中断处理程序 3 抢占功能 4 下半部函数 4.1 内核2.4版本以后的下半部函数: 引入软IRQ 5 ...

  4. spring xml记录

    web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app version="2 ...

  5. (转)Maven实战(二)构建简单Maven项目

    上一节讲了maven的安装和配置,这一节我们来学习一下创建一个简单的Maven项目 1. 用Maven 命令创建一个简单的Maven项目 在cmd中运行如下命令: mvn archetype:gene ...

  6. Javascript:splice()方法实现对数组元素的插入、删除、替换及去重

    定义和用法 splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目. 注释:该方法会改变原始数组. 语法: Array.prototype.splice(index,count[,el ...

  7. 解决html5新标签【placeholder】低版本浏览器下不兼容问题

    placeholder属性是HTML5 中为input添加的.在input上提供一个占位符,文字形式展示输入字段预期值的提示信息(hint),该字段会在输入为空时显示. 实例:1 <input ...

  8. [AngularJS] Angular 1.5 $transclude with named slot

    In Angular 1.5, there is no link and compile. So use if you transclude, you cannot access the fifth ...

  9. Python之基础(一)

    数学计算 要利用相关的数学计算函数,首先需要把数学模块包含进来: >>>import math 进行计算: >>> math.pi 3.14159265358979 ...

  10. linux/unix运行级别

    在SYSTEM V 风格的UNIX系统中,系统被分为不同的运行级别,这和BSD分支的UNIX有所不同,常用的为0~6七个级别:0关机 1单用户 2不带网络的多用户 3带网络的多用户 4保留,用户可以自 ...