• [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 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);
  • 相关阅读:
    1.6 linux基础(六)
    1.5 Linux基础(五)
    1.4 linux基础(四)
    在win10中安装VB的方法
    重新拾起这个博客
    实验11-2-2 学生成绩链表处理
    实验11-1-9 藏尾诗
    实验11-1-8 查找子串
    实验11-1-6 指定位置输出字符串
    实验9-8 通讯录排序
  • 原文地址:https://www.cnblogs.com/Answer1215/p/4198925.html
Copyright © 2020-2023  润新知