Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在express app中需要子模块_Node.js_Express - Fatal编程技术网

Node.js 在express app中需要子模块

Node.js 在express app中需要子模块,node.js,express,Node.js,Express,为什么下面的方法不起作用 app.js reports/ ├── index.js └── batch.js 在app.js中: app.use('/reports', require('./reports') var express = require('express'); var batch = require('./batch'); var app = express.createServer(); ... app.use('/batch', batch); modu

为什么下面的方法不起作用

  app.js
  reports/
  ├── index.js
  └── batch.js
app.js
中:

app.use('/reports', require('./reports')
var express = require('express');
var batch = require('./batch');
var app = express.createServer();
...
app.use('/batch', batch);
module.exports = app;
var express = require('express');
module.exports = function() {
  var app = express.createServer();
  console.log('I am here');
  app.get('/', function(req, res) {
    console.log('I am there');
  });
  return app;
};
index.js
中:

app.use('/reports', require('./reports')
var express = require('express');
var batch = require('./batch');
var app = express.createServer();
...
app.use('/batch', batch);
module.exports = app;
var express = require('express');
module.exports = function() {
  var app = express.createServer();
  console.log('I am here');
  app.get('/', function(req, res) {
    console.log('I am there');
  });
  return app;
};
batch.js
中:

app.use('/reports', require('./reports')
var express = require('express');
var batch = require('./batch');
var app = express.createServer();
...
app.use('/batch', batch);
module.exports = app;
var express = require('express');
module.exports = function() {
  var app = express.createServer();
  console.log('I am here');
  app.get('/', function(req, res) {
    console.log('I am there');
  });
  return app;
};
调用
GET/reports/batch
打印
我在这里
但不打印
我在那里

有人能给我指出这个问题吗


谢谢

这是我的app.js。它将db和app传递给feature.js。基本上,它们共享相同的应用程序变量

在app.js中

    var express = require('express'),
    http = require('http'),
    path = require('path'), 
    mongoose = require('mongoose'),
    db = mongoose.connect('mongodb://abc:123@xxxx.mongohq.com:90000/app123423523');
    var app = express(); 
    //here you set app properties
    require('./routes/feature').with(app, db);
在feature.js中

    module.exports.with = function(app, db) {
                   //do work
    }
试试这个:

在app.js中:

var express = require('express'),
    http = require('http'),
    path = require('path');

var app = express.createServer();

require('./reports')(app);
在reports/index.js中:

module.exports = function(app){
    var batch = require('./batch')(app);

    app.use('/batch', batch);
}
在batch.js中:

module.exports = function(app) {
  console.log('I am here');
  app.get('/', function(req, res) {
    console.log('I am there');
  });
};
请注意,您可能需要修改应用程序。根据需要获取路由。但基本上,这里的想法不是一直调用createServer,而是将其从一个模块传递到下一个模块


希望这有帮助

看来我有点忘记javascript了

我在做什么

app.use('/batch', batch);
批次
等于

function() {
  var app = express.createServer();
  console.log('I am here');
  app.get('/', function(req, res) {
    console.log('I am there');
  });
  return app;
};
相反,我应该这样做

app.use('/batch', batch());

这等于
express.createServer()
返回的是
app.use
希望得到的内容

我实际上更喜欢每次创建express app的模块化。正如您在Express creator上看到的,这是一种常见的模式。我在
index.js
文件中做错了什么,就是搞不清楚我到底把事情搞砸了。我不认为这会有什么不同,但你试过了:
module.exports=function(){var express=require('express');…app.use('/batch',batch);}
@Michael有“模块化”和“代码重用”。视频中的剪切和粘贴操作的数量令人难以置信……对于简单的应用程序来说,每次创建一个express应用程序是可以的,但是在更复杂的应用程序中,需要在不同的阶段完成不同的事情,在某些情况下,对应用程序的更改只需要最少的代码更改即可传递,我仍然认为我的方法是更好的选择。我真的很喜欢有这么多不同的方法来切割节点:)你在使用Express2.x吗
express.createServer()
在express 3中已被弃用。@robertklep是的,不幸的是还没有更新的更改。很多突破性的改变。。。