From 29fe4fd9070f27295069650d53e46abef9e6d51e Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Sat, 30 Mar 2024 04:59:43 +0100 Subject: [PATCH] make sample server available over local network --- packages/sampler/sample-server.mjs | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/sampler/sample-server.mjs b/packages/sampler/sample-server.mjs index e54101d8..0a10fc78 100644 --- a/packages/sampler/sample-server.mjs +++ b/packages/sampler/sample-server.mjs @@ -5,6 +5,7 @@ import { createReadStream } from 'fs'; import { readdir } from 'fs/promises'; import http from 'http'; import { join } from 'path'; +import os from 'os'; console.log( cowsay.say({ @@ -70,7 +71,27 @@ const server = http.createServer(async (req, res) => { readStream.pipe(res); }); -const port = 5432; -server.listen(port, () => { - console.log(`@strudel/sampler is running on http://localhost:${port}`); +const PORT = process.env.PORT || 5432; +const IP_ADDRESS = '0.0.0.0'; +let IP; +const networkInterfaces = os.networkInterfaces(); + +Object.keys(networkInterfaces).forEach((key) => { + networkInterfaces[key].forEach((networkInterface) => { + if (networkInterface.family === 'IPv4' && !networkInterface.internal) { + IP = networkInterface.address; + } + }); +}); + +if (!IP) { + console.error("Unable to determine server's IP address."); + process.exit(1); +} + +server.listen(PORT, IP_ADDRESS, () => { + console.log(`@strudel/sampler is running on + http://localhost:${PORT} + http://${IP}:${PORT} +`); });