app.set('port', port) 'TypeError: undefined is not a function'. Beginner, need ideas

后端 未结 6 942
春和景丽
春和景丽 2021-02-04 04:12

I\'m an amateur learning to build with node.js. I\'ve been following a tutorial to create my first node.js app. It worked perfectly until I entered \'npm start\'. The log is:

6条回答
  •  梦如初夏
    2021-02-04 05:15

    You don't have declared any function called set inside the app.js file. Create that function and export it like this:

    exports.set = function(...) { ... };
    

    If this app is the express app yo especify a port like this:

    var express = require('express'),
        http = require('http');
    
    var app = express();
    
    http.createServer(app).listen(port);
    

    instead of

    app.set('port', port);
     /**
     * Create HTTP server.
     */
    
    var server = http.createServer(app);
    

    This is because the port is a property of the http server and not of the express app

    You can use also

    var express = require('express'),
        app = express();
    
    app.listen(port);
    

提交回复
热议问题