7.5 nodejs笔记
//获取静态资源
app.use(express.static('./public'));
express定义路由的三种方式。http://www.jikexueyuan.com/course/1722_2.html
1、//path
app.get('/',function (req,res) {
res.end(
'hello worl0d \n'
)
})
2、//Router
var Router=express.Router();
/*
http://example.com/post/lst
*/
Router.get('/add',function (req,res) {
res.end('ROuter/add\n');
})
Router.get('/list',function (req,res) {
res.end('router/list\n');
})
app.use('/post',Router);
3、route
app.route('/article')
.get(function (req,res) {
res.end('route/article get\n')
})
.post(function (req,res) {
res.end('route/artcle post\n')
});
运行:curl -X POST http://localhost:3000/article
curl -X GET http://localhost:3000/article
//http://example.com/news/344
//获取344
app.param('newsIdd',function (req,res,next,newids) {
req.idd=newids;
next();
});
app.get('/news/:newsIdd',function (req,res) {
res.end(req.idd+'\n s');
})