3

我为使用 Node.js、express 和 mongoose 的 POST、GET 等方法创建了一个用于不同网页的 API。我还有一个很大的 app.js 文件,其中包含我的 CRUD 方法的路由逻辑和函数。

因此,在 app.js 文件中,我必须为模型文件夹中的每个模型执行 CRUD 功能和路由逻辑。这对于文件来说非常重要,如何将模型的 CRUD 逻辑和路由逻辑分开?这样它仍然可以正常工作而无需冲洗我的文件?

我正在考虑将 CRUD 分离到“控制器”文件夹中,并将路由分离到“路由”文件夹中,但我不知道具体如何,以及在什么地方需要什么..

我的 app.js 看起来像:

var express = require('express');
var app     = express();
var bodyParser = require("body-parser");
var morgan = require("morgan");
var routes = require('./routes');
var cors = require('cors')

//configure app
app.use(morgan('dev')); //log requests to the console

//configure body parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

var port = process.env.PORT || 5000;

//DATABASE SETUP
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/DNZ'); //connect to uor datbaase

//Handle the connection event, get reference to database.
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));

db.once('open', function() {
    console.log("DB connection alive");
});

//DNZ models live here
var FA = require('./models/DNZmodels/FA');
var FP = require('./models/DNZmodels/FP');
//ROUTES FOR OUR API
//=============================================================================

//create our router
var router = express.Router();

//middleware to use for all requests
router.use(function(req, res, next) {
    // do logging
    console.log('Something is happening.');
    console.log('Today is:', Date())
    next();
});

//test route to make sure everything is working (accessed at GET http://localhost:5000/DNZ/)
router.get('/', function(req, res) {
    res.json({ message: 'Welcome to DNZ API!' });   
});

//on routes that end in /FA
//----------------------------------------------------
router.route('/FA')

    // create a FA (accessed at POST http://localhost:8080/DNZ/FA)
    .post(function(req, res) {
        //console.log(req.body);
        //console.log(req.body.params);
        //res.setHeader('Content-Type', 'application/json')
        //res.send(JSON.stringify(req.body));
    /*
        var timestamp = req.body.Timestamp;
        var prognostizierterBetriebswert = req.body.PrognostizierterBetriebswert;
        var posFlexPot = req.body.posFlexPot;
        var negFlexPot = req.body.negFlexPot;
        var leistungsuntergrenze = req.body.Leistungsuntergrenze;
        var leistungsobergrenze = req.body.Leistungsobergrenze;
        var posGesEnergie = req.body.posGesEnergie;
        var negGesEnergie = req.body.negGesEnergie;
        var preissignal = req.body.Preissignal;
        var dummy1 = req.body.Dummy1;
        var dummy2 = req.body.Dummy2;
        var dummy3 = req.body.Dummy3;

        var fa = new FA({
            Timestamp: timestamp,
            Leistungsuntergrenze: leistungsuntergrenze,
            Leistungsobergrenze:leistungsobergrenze,
            PrognostizierterBetriebswert :prognostizierterBetriebswert,
            posFlexPot: posFlexPot,
            negFlexPot:negFlexPot,  
            posGesEnergie: posGesEnergie,
            negGesEnergie: negGesEnergie,
            Preissignal:preissignal,
            Dummy1: dummy1,
            Dummy2: dummy2,
            Dummy3: dummy3          
        })
        */

        //fa.name = req.body.name;
        console.log("Erzeugen der Instanz FA..");
        //console.log(Dummy1);
        //res.send(JSON.stringify(timestamp));

        // create a new instance of the FA model
        var fa = new FA(req.body);      

        //SAVE the new instance
        fa.save(function(err) {
            if (err) {
                console.log(err);
                res.status(400);
                res.send(err);
        }
        else {
            console.log("Instanz FA in Datenbank erzeugt!");
            res.status(200);
            res.json({ message: 'FA-Instance created in datbase!' });
        }
        });

    })

    // get all the FAs (accessed at GET http://localhost:8080/DNZ/FA)
    .get(function(req, res) {
        FA.find(function(err, fas) {
            if (err)
                res.send(err);

            res.json(fas);
        });
    });

//on routes that end in /FA/:FA_id
//----------------------------------------------------
router.route('/FA/:FA_id')

    // get the bear with that id
    .get(function(req, res) {
        FA.findById(req.params.bear_id, function(err, fa) {
            if (err)
                res.send(err);
            res.json(fa);
        });
    })

    /*
     * Athlete.
  find().
  where('sport').equals('Tennis').
  where('age').gt(17).lt(50).  //Additional where query
  limit(5).
  sort({ age: -1 }).
  select('name age').
  exec(callback);
     */
    // update the bear with this id
    .put(function(req, res) {
        FA.findById(req.params.FA_id, function(err, fa) {

            if (err)
                res.send(fa);

            //bear.name = req.body.name;
            /*
            FA.save(function(err) {
                if (err)
                    res.send(err);

                res.json({ message: 'FA updated!' });
            });
            */
        });
    });

    /*
    // delete the bear with this id
    .delete(function(req, res) {
        FA.remove({
            _id: req.params.bear_id
        }, function(err, FA) {
            if (err)
                res.send(err);

            res.json({ message: 'Successfully deleted' });
        });
    });
     */
//*************************************************************************
    //CREATE FP ROUTE
//*************************************************************************
router.route('/FP')

// create a FA (accessed at POST http://localhost:8080/DNZ/FP)
.post(function(req, res) {

    //res.setHeader('Content-Type', 'application/json') 
    console.log("Erzeugen der Instanz FP..");

    // create a new instance of the FP model
    var fp = new FP(req.body);      

    //SAVE the new instance
    fp.save(function(err) {
        if (err) {
            console.log(err);
            res.status(400);
            res.send(err);
    }
    else {
        console.log("Instanz FP in Datenbank erzeugt!");
        res.status(200);
        res.json({ message: 'FP-Instance created in datbase!' });
    }
    });

})

// get all the FAs (accessed at GET http://localhost:8080/DNZ/FA)
.get(function(req, res) {
    FP.find(function(err, fps) {
    if (err) {
        console.log(err);
        res.status(400);
        res.send(err);
    }
    else {
        //res.send("Willkommen auf /FP");
        res.json(fps);
    }
    });
});

//REGISTER OUR ROUTES -------------------------------and listen to requests
app.use('/DNZ', router);

//START THE SERVER
//=============================================================================



// set static directories
app.use(express.static('./dist'));
app.use(cors());

// Define Routes
var index = require('./routes/index');
var users = require('./routes/users');

//Set up routes
routes.init(app)

//run
app.listen(port);
console.log('Listen on port: ' + port);


console.log('Server started, Listening on port ',  port);
4

2 回答 2

0

这主要是基于意见的,但我前段时间也有同样的事情,并开发了一种从主文件中提取路由/模型逻辑的方法,使用模块require-dir

在我的 app.js

/**
 * Process ALL routes from routes dir utilising require-dir
 *
 * Allows us to include an entire directory, without replicating
 * code, creating similar routes on a per end-point basis. This
 * nicely keeps all end-point routes separate.
 */
var routes = requireDir('./routes');
for(var x in routes) {
    application.use('/', routes[x]); // here, application is your express server instance
}

然后创建一个名为routes并添加任何内容的目录,例如routes/branding.js

var expressFramework = require('express');
var Branding = require('../models/branding');
var responseHelper = require('../shared/responseHelper');
var responseStatusCodes = require('../shared/responseStatusCodes');
var responseMessages = require('../shared/responseMessages');
var queryHelper = require('../shared/queryHelper');

var routerObject = expressFramework.Router();
var branding = new Branding();
var responsehelper = new responseHelper();
var queryhelper = new queryHelper();

/**
 * Endpoint /branding/{chain_id}/{site_id}
 */
routerObject.get('/branding/:chain_id/:site_id', function(req, res, next) {
    var requiredFields = [
        {
            chain_id: true, where: 'path'
        },
        {
            site_id: true, where: 'path'
        }
    ];

    if (!queryhelper.authenticateToken(req.headers.authorization)) {
        responsehelper.sendResponse(res, responseStatusCodes.STATUS_UNAUTHORISED,
            responseMessages.RESPONSE_AUTHENTICATION_FAILED);
        return;
    }

    if (!queryhelper.validateQuery(req, requiredFields)) {
        responsehelper.sendResponse(res, responseStatusCodes.STATUS_INVALID_QUERY_PARAMS,
            responseMessages.RESPONSE_INVALID_QUERY_PARAMS);
        return;
    }

    branding.getBranding(req, function(err, results, pagination) {
        if (err) return next(err);

        if (results.length >= 1) {
            responsehelper.sendResponse(res, responseStatusCodes.STATUS_OK, results);
        } else {
            responsehelper.sendResponse(res, responseStatusCodes.STATUS_NOT_FOUND,
                responseMessages.RESPONSE_NO_RECORDS_FOUND);
        }
    });
});

module.exports = routerObject;

这里的关键点是您最终导出了您的应用程序可以“使用”的快速路由器对象。您还会注意到,品牌使用包含var Branding = require('../models/branding');- 这包含所有逻辑,而路由仅包含端点定义,一个片段:

var Promise         = require('bluebird');
var db              = require('../shared/db');
var queryHelper     = require('../shared/queryHelper');
var schemas         = require('../schemas/schemas');
var _               = require('lodash');
var serverSettings  = require('../shared/coreServerSettings');

// Create new instance of mysql module
var connection      = new db();
var queryhelper     = new queryHelper();

var queryAndWait    = Promise.promisify(connection.query);

// Branding Object
var Branding = function(data) {
    this.data = data;
};

Branding.prototype.data = {};

/**
 * Branding methods
 */

Branding.prototype.getBranding = function(req, callback) {
    var params = [];
    var queryFoundRows = `select FOUND_ROWS() as total`;
    var query = `select
        id
        from
        company
        where
        1=1
        and chain_id=?`;

    params.push(req.params.chain_id);


    queryAndWait(query + '; ' + queryFoundRows, params).then(function(result) {
        return Promise.map(result[0], function(row) {
            var params = [];
            var query = `select
                *
                from
                location
                where
                1=1
                and company_id=?
                and site_id=?`;

            params.push(row.id);
            params.push(req.params.site_id);

            return queryAndWait(query, params).then(function(result) {
                return result[0];
            });
        }).then(function(payload) {
            callback(null, payload);
        }).catch(function(err) {
            callback(err, null, null);
        });
    });
};

module.exports = Branding;

可能不完全是您所追求的,但应该提供一个良好的起点。祝你好运!

于 2018-05-16T07:27:05.893 回答
0

然而,这个话题是主观的——重要的是要采取标准并坚持下去。我处理这个问题的方法是创建一个带有模块的子文件夹(使用 module.exports)和一个构造 express 应用程序的 init 函数。

对于每条路由,我都有另一个模块,该模块具有一个 init 函数,该函数接受 express 应用程序作为参数,然后在其中添加路由。

主要代码文件:

var Api = require('./API/index.js');

文件 /API/Index.js:

var express = require('express');
/* Instantiations */
var app = express();
module.exports = {
...
apply(); 
...
}
var route1 = require('./Routes/route1.js');
var route2 = require('./Routes/route2.js');
/* all additional routes */

var Routes = [route1,route2,...]
function apply(){
    for(var i=0;i<Routes.length;i++){
        Routes[i].init(app);
    }
}

然后在 API/Routes/route1.js 和 API/Routes/route2.js...

module.exports = {
    init: function(app){
        /* add your route logic here */
    }
}

根据我的经验,这种方法的好处是您可以根据需要选择添加或删除路由,它通过文件系统提供可读的路由路径,这在大多数现代文本编辑器中都很方便。

于 2018-05-16T10:46:36.457 回答