Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/41.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.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导出nodejs中的变量(var app=express())_Node.js_Post_Express_Module_Export - Fatal编程技术网

Node.js 无法使用express导出nodejs中的变量(var app=express())

Node.js 无法使用express导出nodejs中的变量(var app=express()),node.js,post,express,module,export,Node.js,Post,Express,Module,Export,我需要帮助来解决这个问题 我有一个文件web.js。我在那边 var express = require("express"); var app = express(); var web2 = require("./web2"); /* Code the start the server on the required port*/ app.get('/param1', function(req, res){ console.log("INSIDE GET

我需要帮助来解决这个问题

我有一个文件web.js。我在那边

  var express = require("express");
  var app = express();       
  var web2 = require("./web2");
 /* Code the start the server on the required port*/

 app.get('/param1', function(req, res){
    console.log("INSIDE GET METHOD OF WEB.JS");
 });
 module.exports.app = app
var web = require("./web");
app = web.app;
app.get('/param2', function(req, res){
    console.log("INSIDE GET METHOD OF WEB2.JS");
});
我有另一个文件web2.js。我在那边

  var express = require("express");
  var app = express();       
  var web2 = require("./web2");
 /* Code the start the server on the required port*/

 app.get('/param1', function(req, res){
    console.log("INSIDE GET METHOD OF WEB.JS");
 });
 module.exports.app = app
var web = require("./web");
app = web.app;
app.get('/param2', function(req, res){
    console.log("INSIDE GET METHOD OF WEB2.JS");
});
在开始时,我遇到了一个错误 TypeError:无法调用未定义的方法“post”

如果我从web.js中删除第3行——我可以启动服务器,但是需要http:///param2 给出了一个404

更新的场景:

我正在使用pg数据库,并尝试创建一个客户端,该客户端保存一个客户端实例(在web.js中)。然后我将其传递给另一个文件(web2.js)。在web.js中,我总是将此客户端设置为null

在web.js中,我有以下代码

 var pg = require("pg");
 var pgclient;

app.get('*', function(req,res,next){
   pg.connect(process.env.DATABASE_URL, function(err, client, done) {
     if(client != null){
        pgclient = client;
        console.log("Client connection with Postgres DB is established");
        next();
      }
   }
 }  


 require("./web2.js")(app, pgclient);
module.exports = function(app, pgclient){
   app.get('/param1', function(req,res){
     if(pgclient != null){

     }
     else{
        res.send(500, "pgclient is NULL");
     }
   });
}
在web2.js中,我有以下代码

 var pg = require("pg");
 var pgclient;

app.get('*', function(req,res,next){
   pg.connect(process.env.DATABASE_URL, function(err, client, done) {
     if(client != null){
        pgclient = client;
        console.log("Client connection with Postgres DB is established");
        next();
      }
   }
 }  


 require("./web2.js")(app, pgclient);
module.exports = function(app, pgclient){
   app.get('/param1', function(req,res){
     if(pgclient != null){

     }
     else{
        res.send(500, "pgclient is NULL");
     }
   });
}

代码从未到达web2.js中的if块(if(pgclient!=null))问题在于web.js和web2.js之间的循环依赖关系。当web2.js
require
s web.js时,web.js的
module.exports
尚未设置。我宁愿这样做:

web.js

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

app.get("/param1", function (req, res) {
    // ...
});

require("./web2")(app);

app.listen(/* port number */);
web2.js

module.exports = function (app) {
    app.get("/param2", function (req, res) {
        // ...
    });
};

我有另一个场景,我在问题中更新了这个场景。我相信使用express中间件的标准方法是设置req的属性。因此,在app.get(“*”…)中,您将设置req.pgclient=client,然后在app.get(“/param2”…)中,您将作为req.pgclient访问它。不过,我对编写中间件不太熟悉。