lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.22k stars 86 forks source link

Server shuts down #178

Closed mustafaekim closed 1 year ago

mustafaekim commented 1 year ago

Hello,

I start the server but it does not stay open, rather the script ends right after the server starts up. Please see the code below:

import LocalWebServer from 'local-web-server'
//var LocalWebServer = require("local-web-server");

async function start(){
  //const LocalWebServer = require("local-web-server")
  const ws = await LocalWebServer.create({
    port: 8000,
    directory: '../public',
    hostname: "192.168.1.96"  
  })

  console.log(`Webserver running...`)
};

start();

When I run this peace of code through the "node start-server.js" command, I am seeing the log "Webserver running..." log on the console but right after that the script ends and the server does not live any more.

Does anyone have a guesss why the script is terminated rather than waiting to respond incoming requests?

75lb commented 1 year ago

shouldn't it be await start() at the bottom?

mustafaekim commented 1 year ago

shouldn't it be await start() at the bottom?

Nope, you cannot use await inside the file but only inside an async function.

By the way, the exact same code works in one of my machine (the script never ends, the server runs and respond to requests as expected), on another machine the script immediately terminates as I've written in my previous message and the server does not run anymore. Both of the computers use the same node version and the same npm version of local-web-server. How could this be?

75lb commented 1 year ago

Nope, you cannot use await inside the file but only inside an async function.

You can.. top-level await has been available in Node since at least v14.. You must await your start method.

75lb commented 1 year ago

Closing for now, if you confirm it's definitely a bug in local-web-server let me know..