最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to split Node.js files in several files - Stack Overflow

programmeradmin0浏览0评论

I have this code:

var express = require("express");
var app     = express();
var path    = require("path");

app.use(express.static(__dirname + '/public'));

app.get('/',function(req,res){
  res.sendFile(path.join(__dirname+'/views/index.html'));
  res.set('Access-Control-Allow-Origin', '*');
}).listen(3000);

console.log("Running at Port 3000");

app.get('/test', function(req, res) {
    res.json(200, {'test': 'it works!'})
})

I will have many services (like the test one), and I don't want to have them all on the same file.

I read in another question in Stack Overflow, that I can require other files like this: var express = require("./model/services.js"); And in that file write all the services, but it's throwing app is not defined when I start Node.

How can I separate codes?

I have this code:

var express = require("express");
var app     = express();
var path    = require("path");

app.use(express.static(__dirname + '/public'));

app.get('/',function(req,res){
  res.sendFile(path.join(__dirname+'/views/index.html'));
  res.set('Access-Control-Allow-Origin', '*');
}).listen(3000);

console.log("Running at Port 3000");

app.get('/test', function(req, res) {
    res.json(200, {'test': 'it works!'})
})

I will have many services (like the test one), and I don't want to have them all on the same file.

I read in another question in Stack Overflow, that I can require other files like this: var express = require("./model/services.js"); And in that file write all the services, but it's throwing app is not defined when I start Node.

How can I separate codes?

Share Improve this question asked Nov 26, 2015 at 21:06 PabloPablo 10.6k18 gold badges59 silver badges80 bronze badges 2
  • You can require express in a different file named test.js and have all the test code inside it. Then just export it and call it later in the main app file. – Bwaxxlo Commented Nov 26, 2015 at 21:14
  • But if I required express in the other file, how do I get the instance of the app? – Pablo Commented Nov 26, 2015 at 21:22
Add a comment  | 

2 Answers 2

Reset to default 12

You can define your routes in different files say test-routes.js like this:

module.exports = function (app) {
    app.get('/test', function(req, res) {
        res.json(200, {'test': 'it works!'})
    })
}

Now in your main file say server.js you can import your route file like this:

var express = require("express");
var app     = express();
var path    = require("path");

app.use(express.static(__dirname + '/public'));

app.get('/',function(req,res){
  res.sendFile(path.join(__dirname+'/views/index.html'));
  res.set('Access-Control-Allow-Origin', '*');
}).listen(3000);

console.log("Running at Port 3000");

// import your routes
require('./test-routes.js')(app);

your test.js should look something like:

var express = require('express');
var router = express.Router();

router.get('/test', function (req, res) {
  res.json(200, {'test': 'it works!'});
});

module.exports = router;

and the app.js (assuming other is some other route defined similarly to test.js):

var test = require("./routes/test.js");
var other = require("./routes/other.js");
...
//all your code for creating app 
...
app.use('/test', test);
app.use('/other', other);
发布评论

评论列表(0)

  1. 暂无评论