node.js Router



/**
 * Created by dell on 2016/6/29.
 */

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

app.use(express.static('./public'));
app.use(morgan());

app.get('/',function(req,res,next){
    res.end('Hi liaojianguo');
    next();
});

var Router = express.Router();

Router.get('/add',function(req,res){
    res.end('Router /add');
})

app.use('/post',Router);

app.route('/article')
    .get(function(req,res){
        res.end('route /article get')
    })
    .post(function(req,res){
        res.end('route /article post')
    });

app.param('newId',function(req,res,next,newsId){
    req.newsId = newsId;
    next();
});

app.get('/news/:newId',function(req,res){
    res.end('newsId:' + req.newsId + '\n');
});

Router.get('/list',function(req,res){
    res.end('Router /list');
});

app.listen(18001,function afterListen(){
    console.log('express running on http://localhost:18001');
})


你可能感兴趣的:(NodeJs)