多年来,我一直在 Windows 上使用http-server 。今天我在 ubuntu 上安装了 http-server,但它不起作用。它的初始化与在 Windows 上完全相同。但是,当我导航到本地网站 ( http://127.0.0.1:8080 ) 时出现错误。这在 Windows 上不会发生。知道如何解决这个问题吗?
clayton@u20:~/hello-pwa-master$ http-server
Starting up http-server, serving ./
http-server version: 14.0.0
http-server settings:
CORS: disabled
Cache: 3600 seconds
Connection Timeout: 120 seconds
Directory Listings: visible
AutoIndex: visible
Serve GZIP Files: false
Serve Brotli Files: false
Default File Extension: none
Available on:
http://127.0.0.1:8080
http://192.168.4.54:8080
Hit CTRL-C to stop the server
[Sun Dec 12 2021 22:12:15 GMT-0700 (Mountain Standard Time)] "GET /" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36"
_http_outgoing.js:470
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at ServerResponse.setHeader (_http_outgoing.js:470:11)
at module.exports.ResponseStream.(anonymous function) [as setHeader] (/usr/local/lib/node_modules/http-server/node_modules/union/lib/response-stream.js:100:34)
at Object.exports.(anonymous function) (/usr/local/lib/node_modules/http-server/lib/core/status-handlers.js:57:7)
at Readable.stream.on (/usr/local/lib/node_modules/http-server/lib/core/index.js:339:22)
at Readable.emit (events.js:198:13)
at emitErrorNT (internal/streams/destroy.js:91:8)
at emitErrorAndCloseNT (internal/streams/destroy.js:59:3)
at process._tickCallback (internal/process/next_tick.js:63:19)
好的,找到了解决方案。出于某种奇怪的原因,Ubuntu 20.04 LTS 安装了节点 v10(当前的 LTS 是 v16)。这个版本有好几年了。
我不得不手动卸载节点,因为 apt uninstall 留下了许多孤儿(在 /usr/local/bin、/usr/local/share、/usr/local/lib 和 ~)。
完全删除 v10 后,我使用 curl 和 apt 下载并安装 node v16。
现在,安装了 V16,http-server 可以正常工作。