码迷,mamicode.com
首页 > 其他好文 > 详细

[MEAN] 6. Using Express route instance

时间:2015-01-02 19:52:36      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

For server.js, we update the code by using route instance. By using this, we can remove some duplicate code.

For example:

app.get(‘/people‘, function(request, response){});
app.post(‘/people‘, parseUrlencoded, function(request, response){});
app.get(‘/people/:name‘, function(request, response){});
app.delete(‘/people/:name‘, function(request, response){});

There are a lot of ‘/people/‘...

Using route instance:


app.route(‘/people‘)
    .get(function(req ,res){})
    .post(parseUrlEncoded, function(req, res){});

app.route(‘/people/:name‘)
    .get(function(req, res){})
    .delete(function(req, res){});

 

 

In our case:


 

before:

//server.js

‘use strict‘;

var express = require(‘express‘);
var cors = require("cors");
var app = express();
app.use(cors());
var people = require(‘./controller/people‘);

app.get(‘/people‘, people.getAll);
app.get(‘/people/:id‘, people.get);

app.listen(3000);

after:

‘use strict‘;

var express = require(‘express‘);
var cors = require("cors");
var app = express();
app.use(cors());
var people = require(‘./controller/people‘);

app.route(‘/people‘)
    .get(people.getAll);
app.route(‘/people/:id‘)
    .get(people.get);
app.listen(3000);

 

[MEAN] 6. Using Express route instance

标签:

原文地址:http://www.cnblogs.com/Answer1215/p/4198686.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!