码迷,mamicode.com
首页 > Windows程序 > 详细

[MEAN Stack] First API -- 7. Using Route Files to Structure Server Side API

时间:2015-01-02 23:36:06      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

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 middleware var router = express.Router(); router.route(‘/‘) .get(people.getAll); router.route(‘/:id‘) .get(people.get); //exports the router as a Node module module.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 middleware
app.use(‘/people‘, people);

app.listen(3000);

 

[MEAN Stack] First API -- 7. Using Route Files to Structure Server Side API

标签:

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

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