dev-server.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const express = require("express");
  2. const { generateDevHtml, build } = require("./utils.js");
  3. const { argv } = require('node:process');
  4. const chalk = require('chalk');
  5. const app = express();
  6. let port = 4000; // Starting port
  7. const maxAttempts = 10; // Maximum number of ports to try
  8. const env = argv[2] ?? "dev";
  9. const startServer = (attempt) => {
  10. if (attempt > maxAttempts) {
  11. console.error(chalk.red(`ERROR: Unable to find an available port after ${maxAttempts} attempts.`));
  12. return;
  13. }
  14. app.listen(port, () => {
  15. console.log("\n-----------------------------------------------------------\n");
  16. console.log(`Puter is now live at: `, chalk.underline.blue(`http://localhost:${port}`));
  17. console.log("\n-----------------------------------------------------------\n");
  18. }).on('error', (err) => {
  19. if (err.code === 'EADDRINUSE') { // Check if the error is because the port is already in use
  20. console.error(chalk.red(`ERROR: Port ${port} is already in use. Trying next port...`));
  21. port++; // Increment the port number
  22. startServer(attempt + 1); // Try the next port
  23. }
  24. });
  25. };
  26. // Start the server with the first attempt
  27. startServer(1);
  28. // build the GUI
  29. build();
  30. app.get(["/", "/app/*", "/action/*"], (req, res) => {
  31. res.send(generateDevHtml({
  32. env: env,
  33. api_origin: "https://api.puter.com",
  34. title: "Puter",
  35. max_item_name_length: 150,
  36. require_email_verification_to_publish_website: false,
  37. short_description: `Puter is a privacy-first personal cloud that houses all your files, apps, and games in one private and secure place, accessible from anywhere at any time.`,
  38. }));
  39. })
  40. app.use(express.static('./'));
  41. if(env === "prod"){
  42. // make sure to serve the ./dist/ folder maps to the root of the website
  43. app.use(express.static('./dist/'));
  44. }
  45. if(env === "dev"){
  46. app.use(express.static('./src/'));
  47. }
  48. module.exports = app;