首页 > 代码库 > [MEAN Stack] First API -- 7. Using Route Files to Structure Server Side API
[MEAN Stack] First API -- 7. Using Route Files to Structure Server Side API
Currently, the server.js is going way too long. In the real world application, it is likely that we are going to deal with more routers, whichi means it growing even longer.
A single file which has too many lines of code whcih means code small.
We are going to extract routes to modules.
firstMean/routes/people.js:
/** * Created by Answer1215 on 1/2/2015. */var express = require(‘express‘);var people = require(‘../controller/people‘);
//return router instance which can be mounted as a middlewarevar router = express.Router();router.route(‘/‘) .get(people.getAll);router.route(‘/:id‘) .get(people.get);//exports the router as a Node modulemodule.exports = router;
Server.js:
‘use strict‘;var express = require(‘express‘);var cors = require("cors");var app = express();app.use(cors());var people = require(‘./routes/people‘);//use router as a middlewareapp.use(‘/people‘, people);app.listen(3000);
[MEAN Stack] First API -- 7. Using Route Files to Structure Server Side API
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。