javascript - app.set('port',8080)与Express.js中的app.listen(8080) [英] javascript - app.set('port', 8080) versus app.listen(8080) in Express.js

查看:688
本文介绍了javascript - app.set('port',8080)与Express.js中的app.listen(8080)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Express.js来推出一个网站。起初,我使用
app.set('port',8080),但浏览器无法连接到该页面。之后,我将代码更改为 app.listen(8080),网页正常显示。

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, I changed the code to app.listen(8080) and the webpage appeared normally.

这导致我想知道,这两个函数有什么区别?

This led me to wonder, what is the difference between these two functions?

推荐答案

app.set('port' ,8080)类似于将名为 port 的变量设置为 8080 ,其中您可以稍后使用 app.get('port')进行访问。从浏览器访问您的网站实际上不起作用,因为您仍然没有告诉您的应用程序听取和接受连接。

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 的连接。这是您告诉应用听取和接受连接的部分。如果你的代码中有这个,那么使用 localhost:8080 从浏览器访问你的应用程序将会有效。

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'));

这篇关于javascript - app.set('port',8080)与Express.js中的app.listen(8080)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆