ng serve 在 Docker 容器中不工作,但 express.js 可以
ng serve not working in Docker container but express.js does
我读过类似的问题 ,但我不明白为什么我必须指定主机 0.0.0.0
到 运行 ng serve
。为了进行比较,我 运行 一个简单的脚本 express.js 在同一端口的同一容器中,在这种情况下,网站在 localhost:8080 没有定义 0.0.0.0主机.
据我了解,两台服务器(ng serve
和 express.js
)的工作方式相同。 运行 都在容器内,但只能在浏览器中访问 express,我很困惑为什么 ng serve
不能。
重现步骤:
- 运行 LTS Node.js 容器,名称为
angular-test
来自 CLI,发布端口为 8080:4200
docker container run --name angular-test -it -v ${PWD}:/app/ -p 8080:4200 node:lts /bin/bash
- 连接到容器的终端
docker container attach angular-test
- 转到 angular 项目和 运行
ng serve
- 转到浏览器
localhost:8080
但 returns “连接已重置页面加载时与服务器的连接已重置。...`
- 停止
ng serve
- 运行
node test-express
在同一端口上启动 express.js 服务器 4200
- 转到浏览器
localhost:8080
并且网站加载没有任何问题,文本 Express.js works!
可见
const express = require('express')
const app = express()
const port = 4200
app.get('/', (req, res) => {
res.send('Express.js works!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
这可能无关紧要,但我在 WSL2 (Windows 10 Pro) 中为 Windows 使用 Docker。
Express.js 利用 NodeJs 的方法
server.listen([port[, host[, backlog]]][, callback])
see documentation
If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
如果未指定主机,Node.js 将未指定的 IP 地址 ::
或 0.0.0.0
设置为默认值。如果您想知道哪个主机被默认,请在 app.listen()
中调用 server.address()
,其中 server
是由 http.Server which extends net.Server 编辑的对象 return。
const port = 4200;
const server = app.listen(port, () => {
console.log('App listening at', server.address())
})
server.address()
应该 return 一个对象,在我的例子中是
{ address: '::', family: 'IPv6', port: 4200 }
现在很清楚 express.js 正在监听 ::
,而不是我预期的 localhost
。
我读过类似的问题 0.0.0.0
到 运行 ng serve
。为了进行比较,我 运行 一个简单的脚本 express.js 在同一端口的同一容器中,在这种情况下,网站在 localhost:8080 没有定义 0.0.0.0主机.
据我了解,两台服务器(ng serve
和 express.js
)的工作方式相同。 运行 都在容器内,但只能在浏览器中访问 express,我很困惑为什么 ng serve
不能。
重现步骤:
- 运行 LTS Node.js 容器,名称为
angular-test
来自 CLI,发布端口为 8080:4200
docker container run --name angular-test -it -v ${PWD}:/app/ -p 8080:4200 node:lts /bin/bash
- 连接到容器的终端
docker container attach angular-test
- 转到 angular 项目和 运行
ng serve
- 转到浏览器
localhost:8080
但 returns “连接已重置页面加载时与服务器的连接已重置。...` - 停止
ng serve
- 运行
node test-express
在同一端口上启动 express.js 服务器4200
- 转到浏览器
localhost:8080
并且网站加载没有任何问题,文本Express.js works!
可见
const express = require('express')
const app = express()
const port = 4200
app.get('/', (req, res) => {
res.send('Express.js works!')
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})
这可能无关紧要,但我在 WSL2 (Windows 10 Pro) 中为 Windows 使用 Docker。
Express.js 利用 NodeJs 的方法
server.listen([port[, host[, backlog]]][, callback])
see documentation
If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available, or the unspecified IPv4 address (0.0.0.0) otherwise.
如果未指定主机,Node.js 将未指定的 IP 地址 ::
或 0.0.0.0
设置为默认值。如果您想知道哪个主机被默认,请在 app.listen()
中调用 server.address()
,其中 server
是由 http.Server which extends net.Server 编辑的对象 return。
const port = 4200;
const server = app.listen(port, () => {
console.log('App listening at', server.address())
})
server.address()
应该 return 一个对象,在我的例子中是
{ address: '::', family: 'IPv6', port: 4200 }
现在很清楚 express.js 正在监听 ::
,而不是我预期的 localhost
。