如何将此文件拆分为node.js中的多个文件

时间:2015-02-02 05:50:32

标签: javascript node.js

所以我想在node.js服务器中找到一些文件,因为我觉得它们混乱了。知道我怎么能做到这一点吗?

我想要一个文件包含这样的内容 理想情况下这样的东西可以工作(文件的内容可以只是附加到文件的顶部,或者其他什么,通过某种神奇的需求声明)

settings.js文件

/* Defines a bunch of global variables and settings */
/* I understand global isa  bad practice but I want this clutter out of the main server file */


express = require("express"); // Express JS
logfmt = require("logfmt"); // Heroku key-value logger
mongoose = require('mongoose'); // DB simplifier
passport = require('passport'); // Authentication
flash    = require('connect-flash'); // Flash Messages
port = Number(process.env.PORT || 5000); // Locally uses port 5000, else uses port of server
configDB      = require('./config/database.js');
cookieParser  = require('cookie-parser');
bodyParser    = require('body-parser');
session       = require('express-session');

app = express(); //Express for server

主服务器文件

/ ********************************************** ****  **服务器  ************************************************** /

var settings = require("./settings");

/* GLOBAL VARIABLE FOR THE SERVER. MAYBE BAD PRACTICE IDK YOLO */ 
server        = require('http').createServer(app);
util          = require("util");
io  = require("socket.io").listen(server);
 _settings = new settings();
//socket;   // Socket controller idk tbh
/* END GLOBAL VARIABLES */
....

1 个答案:

答案 0 :(得分:-1)

我明白了!

/ *定义一堆全局变量和设置* /

settings.js

module.exports 

= {
  load : function() {
           express = require("express"); // Express JS
           logfmt = require("logfmt"); // Heroku key-value logger
           mongoose = require('mongoose'); // DB simplifier
           passport = require('passport'); // Authentication
           flash    = require('connect-flash'); // Flash Messages
           port = Number(process.env.PORT || 5000); // Locally uses port 5000, else uses port of server
           configDB      = require('./config/database.js');
           cookieParser  = require('cookie-parser');
           bodyParser    = require('body-parser');
           session       = require('express-session');

           app = express(); //Express for server
         }
};

server.js

var settings = require("./settings");
settings.load();

//:D