admin管理员组文章数量:1425717
I have a growing node.js server application, and I'd like to get it split into several files. Here is a working code snippet demonstrating what monolithic server.js roughly looks like:
var express = require('express');
var app = express();
// other initialization code etc
//************** start of part to be moved foo.js
var fooTestData = {data: "data", id:1};
app.get("/foo/ajax", function(req, res) {
res.json(fooTestData);
});
// more REST stuff and other foo-specific code
//************** end of part to be moved
// more stuff which remains in server.js
// http://localhost:8888/foo/ajax
app.listen(8888);
An ideal answer has two pieces of code with identical functionality: What server.js looks like after moving the indicated part, and what foo.js looks like with copied code and any needed extra code.
I have a growing node.js server application, and I'd like to get it split into several files. Here is a working code snippet demonstrating what monolithic server.js roughly looks like:
var express = require('express');
var app = express();
// other initialization code etc
//************** start of part to be moved foo.js
var fooTestData = {data: "data", id:1};
app.get("/foo/ajax", function(req, res) {
res.json(fooTestData);
});
// more REST stuff and other foo-specific code
//************** end of part to be moved
// more stuff which remains in server.js
// http://localhost:8888/foo/ajax
app.listen(8888);
An ideal answer has two pieces of code with identical functionality: What server.js looks like after moving the indicated part, and what foo.js looks like with copied code and any needed extra code.
Share Improve this question asked Sep 30, 2013 at 13:28 hydehyde 63k22 gold badges125 silver badges179 bronze badges 4- 1 nodejs/api/modules.html – JohnnyHK Commented Sep 30, 2013 at 13:39
- @JohnnyHK Thanks for the link, it's useful, though I was after a clear before (in question) - after (in answer) example. Which I got. – hyde Commented Sep 30, 2013 at 13:47
- See my reply to the question here: stackoverflow./questions/18789864/… – Slavo Commented Sep 30, 2013 at 15:25
- About suggested duplicate: I think that question is a bit different, about how to generally structure a node.js application. This question has much tighter scope, it is about a concrete pattern with example to convert part of an existing monolithic source to a new module. – hyde Commented Oct 1, 2013 at 5:59
2 Answers
Reset to default 7foo.js
var fooTestData = {data: "data", id:1};
exports.setApp = function (app) {
app.get("/foo/ajax", function(req, res) {
res.json(fooTestData);
});
// more REST stuff and other foo-specific code
};
new sever.js
var express = require('express');
var app = express();
require('./foo.js').setApp(app);
// other initialization code etc
// more stuff which remains in server.js
// http://localhost:8888/foo/ajax
app.listen(8888);
It's a simple example. If you need some things more plex you may want to take a look to other express program on github. It could be a good source of inspiration.
I remend you to have a look at some express-based (MVC) frameworks. Have a look at the list of frameworks. Moreover, you might want to check sails.js (express based framework) if you are doing a webservice and municating with it via RESTfull requests. Please note that sails is not limited to RESTfull requests and can help you scale your project out of the box.
Good luck!
本文标签: How to split monolithic nodejs javascriptStack Overflow
版权声明:本文标题:How to split monolithic node.js javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745454239a2659027.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论