简体   繁体   English

Node.js Express模块​​导出

[英]Node.js Express module exports

im getting this error 我收到这个错误

app.get is not a function app.get不是一个函数

this is my config/express.js 这是我的config / express.js

var express = require('express');


    module.exports = function(){

    var app = express();

    app.set('port',3000);

    return app;

    };

and this is my server.js 这是我的server.js

var http = require ('http');

var app = require ('./config/express');


http.createServer(app).listen(app.get('port'), function(){

    console.log("Express Server Runing on port"+ app.get('port'));


});

what im doing wrong? 我在做什么错?

in config/express.js you export a function but use it as an app. config/express.js您导出函数但将其用作应用程序。 Try this instead: 尝试以下方法:

var express = require('express');
var app = express();
app.set('port', 3000);
module.exports = app;

Edit: But the following is preferred: 编辑:但是以下是首选:

/* config/express.js */
var express = require('express');
module.exports = function() {
  var app = express();
  app.set('port', 3000);
  return app;
};


/* server.js */
var http = require('http');
var app = require('./config/express')(); // Notice the additional () here

http.createServer(app).listen(app.get('port'), function() {
    console.log("Express Server Runing on port"+ app.get('port'));
});

Whenever you use Nodejs and express js or anyother first check your Versions . 每当您使用Nodejs并表达js或其他任何方法时,请先检查您的Versions。 that can save a lot of time . 这样可以节省很多时间。 Like if you move from express 3 to express 4 so you better check versions 就像您将Express 3换成Express 4一样,以便更好地检查版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM