Saturday, February 11, 2023

Script to Open Local File on Http Server

# Check if http-server is installed

if ! [ -x "$(command -v http-server)" ]; then

  # Install http-server if not installed

  npm install -g http-server

fi


# Start the http-server and serve the current directory

http-server -p 8080 &


# Wait for the server to start

sleep 5


# Open the local file in Chrome

open -a "Google Chrome" http://localhost:8080/index.html

In this script, http-server is started with the -p option to specify the port as 8080, and the current directory is served using http-server .. The URL for the local index.html file is then opened in Chrome using open.

the http-server will look for the index.html file in the folder where it is run. In the script, http-server . starts the server and serves the files in the current directory. So, the script is looking for an index.html file in the same folder where the script is run. When the URL http://localhost:8080/index.html is opened in Chrome, the browser will request the file from the local web server, and the server will return the content of the index.html file if it exists in the current directory.