Using NPX Http-Server
Using NPX Http-Server
Introduction
http-server is a simple, zero-configuration command-line HTTP server that can serve static
files. It is useful for serving web applications during development and testing. You can
quickly set it up using npx, which allows you to run Node.js packages without installing them
globally.
Prerequisites
Ensure you have Node.js installed. You can download it from nodejs.org.
Basic understanding of the command line.
First, create a directory for your project and navigate into it:
mkdir my-http-server
cd my-http-server
npx http-server
By default, this command will serve the files in the current directory on port 8080. You
should see output similar to:
Open your web browser and go to http://localhost:8080. You should see your
index.html file being served, displaying "Hello, World!" and the accompanying text.
You can customize the behavior of http-server with various command-line options. Here
are a few common options:
Enable CORS: Use the --cors option to enable Cross-Origin Resource Sharing:
Show Directory Listings: If you want to enable directory listing, use the -a option:
To stop the server, simply go back to your terminal and press CTRL + C.
Conclusion
Using npx http-server is a quick and easy way to serve static files for development
purposes. Its simplicity and flexibility make it a great tool for quickly testing web applications
without the need for complex configurations. Whether you are working on a simple static site
or a more complex project, http-server can help streamline your workflow.