英文:
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.js
或npm 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('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(`Der erste Server wurde mit dem Code ${code} beendet!`);
});
const roomServerPath = path.join(__dirname, 'room/index.js');
const roomServerProcess = spawn('node', [roomServerPath], { env: { PORT: 5000 } });
roomServerProcess.on('exit', (code) => {
console.log(`Der zweite Server wurde mit dem Code ${code} beendet!`);
});
const port = process.env.PORT || 3000;
app.listen(port, () => {
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
> 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...
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论