如何在我的节点应用程序中使用MVC和包含文件

我有一个节点的应用程序,我有三个文件,并试图构build我的节点在MVC模式。

我想要一个方法来把我所有的需求和variables放在我的server.js中的所有路由在我的routes.js和我的controller.js中的函数。

我的路由器很好,工作。 我如何在我的服务器文件中包含控制器的function

我有:

  • server.js
var configure = require('./router'); var express = require('express'); var app = express(); var port = process.env.PORT || 8080; // get an instance of router var router = express.Router(); configure(router); var request = require('request'); var nodePardot = require('node-pardot'); var bodyParser = require('body-parser'); var rp = require('request-promise'); // Start the server app.listen(port); app.use(bodyParser.json()); // support json encoded bodies app.use(bodyParser.urlencoded({extended: true})); // support encoded bodies console.log('Test server started! At http://localhost:' + port); // Confirms server start // // // START THE SERVER // // ============================================== app.listen(port); console.log('Server has started!! ' + port); // apply the routes to our application app.use('/', router); 

  • router.js`
  module.exports = function (router) { // route middleware that will happen on every request router.use(function (req, res, next) { // log each request to the console console.log(req.method, req.url); // continue doing what we were doing and go to the route next(); }); // home page route (http://localhost:8080) router.get('/', function (req, res) { res.send('im the home page!'); }); router.get('/login', function (req, res) { res.send('this is the login form'); }) // process the form (POST http://localhost:8080/login) .post('/login', function (req, res) { console.log('processing'); // shows on console when post is made res.send('processing the login form!'); // output on postman }); }; 

  • controller.js
  var password = 'gf.09'; var userkey = 'dfgg'; var emailAdmin = 'rt.r@rt.co.uk'; // Start the server app.listen(port); app.use(bodyParser.json()); // support json encoded bodies app.use(bodyParser.urlencoded({extended: true})); // support encoded bodies console.log('Test server started! At http://localhost:' + port); // Confirms server start var firstFunction = function () { return new Promise (function (resolve) { setTimeout(function () { app.post('/back-end/test', function (req, res) { console.log(req.body); var login = req.body.LoginEmail; res.send(login); resolve({ data_login_email: login }); }); console.error("First done"); }, 2000); }); }; var secondFunction = function () { return new Promise (function (resolve) { setTimeout(function () { nodePardot.PardotAPI({ userKey: userkey, email: emailAdmin, password: password, DEBUG: false }, function (err, client) { if (err) { // Authentication failed console.error("Authentication Failed", err); } else { // Authentication successful var api_key = client.apiKey; console.log("Authentication successful !", api_key); resolve({data_api: api_key}); } }); console.error("Second done"); }, 2000); }); }; function thirdFunction(result) { return new Promise (function () { setTimeout(function () { var headers = { 'User-Agent': 'Super Agent/0.0.1', 'Content-Type': 'application/x-www-form-urlencoded' }; // Configure the request var api = result[1].data_api; var login_email = result[0].data_login_email; var options = { url: 'https://pi.pardot.com/api/prospect/version/4/do/read', method: 'POST', headers: headers, form: { 'email': login_email, 'user_key': userkey, 'api_key': api }, json: true // Automatically stringifies the body to JSON }; // Start the request rp(options) .then(function (parsedBody) { console.error(login_email, "Is a user, login pass!"); }) .catch(function (err) { console.error("fail no such user"); // res.status(400).send() }); console.error("Third done"); }, 3000); } ); } // sequence of functions Promise.all([firstFunction(), secondFunction()]) .then(thirdFunction); 

我曾经尝试过

 var routers = require('./router'); var controller = require('./test'); // var controller = require('./test.js','./router' ); var express = require('express'); var request = require('request'); var nodePardot = require('node-pardot'); var bodyParser = require('body-parser'); var rp = require('request-promise'); var app = express(); var port = process.env.PORT || 8080; var router = express.Router(); routers(router); controller(Promise); 

 module.exports = function (Promise) { all functions } 

问题是一些variables是不可用的controller.js所以我得到的错误,如:

app.post('/ back-end / controller',function(req,res){

  ^ 

这是死的简单使用相同的逻辑,你在你的router.js应用。

在controller.js中使用下面的内容:

 exports.function_name = function(params, ...){ . . . } 

然后通过导入controller.js来访问这些函数

 var controller = require('./controller.js'); controller.function_name(param..) # execute with required params and callback 

如果你想返回一个对象,那么你可以做module.exports

 module.exports = Promise.all([firstFunction(), secondFunction()]) .then(thirdFunction); 

控制器对象现在包含一个承诺

 var controller = require('./controller.js'); 

控制器对象可以直接作为承诺,现在不需要调用函数。

一个更好的select是从controller.js中导出所有的函数,然后在app.js中,你可以使用promise.js。

 Promise.all([controller.firstFunction(), controller.secondFunction()]) .then(controller.thirdFunction);