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

javascript - Node.js Express module exports - Stack Overflow

programmeradmin2浏览0评论

im getting this error

app.get is not a function

this is my 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

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?

im getting this error

app.get is not a function

this is my 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

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?

Share Improve this question asked Feb 13, 2017 at 13:30 JohnJohn 5333 gold badges9 silver badges19 bronze badges 1
  • are you sure require ('./config/express') is the correct path? If you log app does it give you something? – Laurens Kling Commented Feb 13, 2017 at 13:33
Add a ment  | 

2 Answers 2

Reset to default 17

in config/express.js you export a function but use it as an app. 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 . that can save a lot of time . Like if you move from express 3 to express 4 so you better check versions

发布评论

评论列表(0)

  1. 暂无评论