nodejs之express中间件路由使用

/*
* 中间件:就是匹配路由之前和匹配路由之后做的一系列操作
*/

var express = require('express');
var app = new express();

/**
* 内置中间件:托管静态页面
*/
//http://localhost:8001/news
app.use(express.static('public'));

//虚拟目录 http://localhost:8001/static/news
app.use('/static',express.static('public'));

/**
* 中间件:表示匹配任何路由
* 应用级中间件
* next() 路由继续向下匹配
*/
app.use(function (req,res,next) {
console.log(new Date());
next();
})

app.get('/',function (req,res) {
res.send("hello express");
})

app.get('/news',function (req,res,next) {
//res.send("hello news");
console.log("news");
next();
})

app.get('/news',function (req,res) {
res.send("hello news1");
})

app.listen("8001");

猜你喜欢

转载自www.cnblogs.com/ywjfx/p/10403913.html