javascript - app.set('port', 8080) versus app.listen(8080) in Express.js

后端 未结 3 1537
半阙折子戏
半阙折子戏 2021-02-04 02:20

I\'ve been trying to use Express.js to launch a website. At first, I was using app.set(\'port\', 8080) but the browser was unable to connect to the page. Afterwards

3条回答
  •  余生分开走
    2021-02-04 03:08

    app.set('port', 8080) is similar to setting a "variable" named port to 8080, which you can access later on using app.get('port'). Accessing your website from the browser will really not work because you still didn't tell your app to listen and accept connections.

    app.listen(8080) on the other hand listens for connections at port 8080. This is the part where you're telling your app to listen and accept connections. Accessing your app from the browser using localhost:8080 will work if you have this in your code.

    The two commands can actually be used together:

    app.set('port', 8080);
    app.listen(app.get('port'));
    

提交回复
热议问题