NodeJS的spawn子进程在glitch.com上无法正常工作。

huangapple go评论69阅读模式
英文:

nodeJS spawn child_process doesn't work on glitch.com

问题

我想在glitch.com上使用以下代码提供一个express服务器:

const path = require('path');
const { spawn } = require('child_process');
const express = require('express');

const app = express();
app.use(express.static('public'));

const defaultServerPath = path.join(__dirname, 'default/index.js');
const defaultServerProcess = spawn('node', [defaultServerPath], { env: { PORT: 4000 } });

defaultServerProcess.on('exit', (code) => {
  console.log(`The first server exited with code ${code}!`);
});

const roomServerPath = path.join(__dirname, 'room/index.js');
const roomServerProcess = spawn('node', [roomServerPath], { env: { PORT: 5000 } });

roomServerProcess.on('exit', (code) => {
  console.log(`The second server exited with code ${code}!`);
});

const port = process.env.PORT || 3000;
app.listen(port, () => {
  console.log(`Reverse proxy server running on ${port}`);
});

它在本地通过使用node index.jsnpm run start运行脚本可以工作,但在glitch上不行。

我在https://glitch.com/上得到这个错误消息。

app@indigo-midi-sushi:~ 08:54 
$ npm run start

> express-socket_chatroom@1.0.0 start /app
> nodemon index.js

[nodemon] 2.0.22
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: spawn node ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    [... lines matching original stack trace ...]
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
[nodemon] app crashed - waiting for file changes before starting...

我知道进程的生成导致了这个错误,但为什么呢? spawn('node', [<someServerPath>], { env: { PORT: <somePort> }

英文:

I want to serve an express server on glitch.com with the following code:

const path = require(&#39;path&#39;);
const { spawn } = require(&#39;child_process&#39;);
const express = require(&#39;express&#39;);

const app = express();
app.use(express.static(&#39;public&#39;));

const defaultServerPath = path.join(__dirname, &#39;default/index.js&#39;);
const defaultServerProcess = spawn(&#39;node&#39;, [defaultServerPath], { env: { PORT: 4000 } });

defaultServerProcess.on(&#39;exit&#39;, (code) =&gt; {
  console.log(`Der erste Server wurde mit dem Code ${code} beendet!`);
});

const roomServerPath = path.join(__dirname, &#39;room/index.js&#39;);
const roomServerProcess = spawn(&#39;node&#39;, [roomServerPath], { env: { PORT: 5000 } });

roomServerProcess.on(&#39;exit&#39;, (code) =&gt; {
  console.log(`Der zweite Server wurde mit dem Code ${code} beendet!`);
});

const port = process.env.PORT || 3000;
app.listen(port, () =&gt; {
  console.log(`Reverse proxy server running on ${port}`);
});

It works locally by running the script with node index.js or npm run start but not on glitch.

I get this error message on https://glitch.com/.
app@indigo-midi-sushi:~ 08:54
$ npm run start

&gt; express-socket_chatroom@1.0.0 start /app
&gt; nodemon index.js

[nodemon] 2.0.22
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
events.js:174
      throw er; // Unhandled &#39;error&#39; event
      ^

Error: spawn node ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Emitted &#39;error&#39; event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    [... lines matching original stack trace ...]
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
[nodemon] app crashed - waiting for file changes before starting...

I know that the spawning of the processes causes the error, but why?
spawn('node', [<someServerPath>], { env: { PORT: <somePort> }

答案1

得分: 0

原来在 glitch 上不能暴露多个端口。

英文:

Turns out you cannot expose more than one port on glitch.

huangapple
  • 本文由 发表于 2023年4月17日 17:03:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033423.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定