NodeJS SvelteKit节点生产应用程序中的自定义端口号

dauxcl2d  于 9个月前  发布在  Node.js
关注(0)|答案(1)|浏览(103)

根据文档,为了给我的SvelteKit应用程序使用自定义端口号,我应该安装dotenv,然后将其添加到我的.env文件中:

HOST=127.0.0.1 PORT=3003 node build

字符串
然后,我用以下代码构建我的应用程序:

node -r dotenv/config build


我看到这个错误:

node:events:492
      throw er; // Unhandled 'error' event
      ^

Error: getaddrinfo ENOTFOUND 127.0.0.1 PORT=3003 node build
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:26)
Emitted 'error' event on Server instance at:
    at GetAddrInfoReqWrap.doListen [as callback] (node:net:2066:12)
    at GetAddrInfoReqWrap.onlookup [as oncomplete] (node:dns:107:17) {
  errno: -3008,
  code: 'ENOTFOUND',
  syscall: 'getaddrinfo',
  hostname: '127.0.0.1 PORT=3003 node build'


如果我忽略这一点,执行常规的npm build并将构建推送到Node服务器,并尝试使用pm2启动它:

pm2 start index.js


.它抛出一个错误,因为端口3000(SvelteKit默认)正在被我的其他应用程序使用。
但是如果我绕过所有这些诡计,并在生产服务器上修改index.js以手动更改端口,应用程序就可以工作了!

const path = env('SOCKET_PATH', false);
const host = env('HOST', '0.0.0.0');
const port = env('PORT', !path && '3003'); //<-- Manually edit here like a caveman 😬

const server = polka().use(handler);

server.listen({ path, host, port }, () => {
    console.log(`Listening on ${path ? path : host + ':' + port}`);
});

export { host, path, port, server };


我做错了什么?如何在第一次构建应用时让应用烘焙自定义端口号?

hfsqlsce

hfsqlsce1#

由于错误提到了这一行:

hostname: '127.0.0.1 PORT=3003 node build'

字符串
这意味着节点认为主机名是整个字符串'127.0.0.1 PORT=3003 node build'
除非我弄错了,.env文件应该看起来更像这样:

HOST=127.0.0.1
PORT=3003


所以每个变量都在自己的行上。它应该只包含变量,所以没有nodebuild

相关问题