首页 > 代码库 > [MEAN] 6. Using Express route instance
[MEAN] 6. Using Express route instance
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
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。