node express route demo

来源:互联网 发布:java hello world 代码 编辑:程序博客网 时间:2024/06/09 19:43
var express = require('express');
var app = express();

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

// http://localhost:8000/
app.get('/',function(req,res){
    res.send('hello\n');
});

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

// http://localhost:8000/post/add
// http://localhost:8000/post/list
var Router = express.Router();

Router.get('/add',function(req,res){
    res.send('hello\n');
});
Router.get('/list',function(req,res){
    res.send('hello\n');
});

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

// get post method requset http://localhost:8000/haha
app.route('/haha')
    .get(function(req,res){
        res.send('route /haha get \n');
    })
    .post(function(req,res){
        res.send('route /haha post\n');
    });

//http://localhost:8000/news/123
app.param('newId',function(req,res,next,newId){
    req.newId = newId;
    next();
});

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