How to split monolithic node.js javascript

前端 未结 2 791
感情败类
感情败类 2021-01-20 18:39

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 serv

2条回答
  •  无人共我
    2021-01-20 18:52

    foo.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 complex you may want to take a look to other express program on github. It could be a good source of inspiration.

提交回复
热议问题