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
> [email protected] 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', [], { env: { PORT: }
Turns out you cannot expose more than one port on glitch.