我要实现的是将应用程序.js分成单独的部分。到目前为止,这是成功的。到目前为止,我的结构如下所示:
package.json
app.js
app/
- server/
- views/
- router.js
- public/
- css/
- images/
- js/
- robots.txt
听上去很好?在我的应用程序中.js我有以下代码:
http.createServer(app).listen(app.get('port'), function() {
console.log("app:" + app.get('port') + " running through express server.");
})
我觉得我的http:createServer太小了,太脆弱了,我想扩展它。有没有办法把它放在./app/server/http.js
里面并包含太忙的模块(这些示例对我来说似乎太难了)。
有解决办法吗?
正确理解了您的问题,您需要做的就是创建一个app/server/http.js
文件并将以下代码放在其中:
var toobusy = require('toobusy'),
app = require('../../app');
var server = http.createServer(app);
server.listen(app.get('port'), function () { ... });
process.on('SIGINT', function() {
server.close();
// calling .shutdown allows your process to exit normally
toobusy.shutdown();
process.exit();
});
然后,您可以执行以下两项操作之一:
1) 在package.json
文件中,将main
字段设置为 app/server/http.js
,如果这是您的事,您可以使用 npm start
启动您的应用程序。
2)另一种选择(首选,IMO)是在项目的根目录中创建一个index.js
文件,如下所示:
module.exports = require('app/server/http');
然后你可以简单地启动你的服务器
$ NODE_ENV=production node /path/to/project <arguments go here>
无论哪种方式,您都将获得toobusy
的好处,同时实现您想要的分离。