Apps Artificial Intelligence CSS DevOps Go JavaScript Laravel Linux MongoDB MySQL PHP Python Rust Svelte Vue

Create a Simple Static Web Server with `http-server`

1 min read .
Create a Simple Static Web Server with `http-server`

When you need to test static HTML, CSS, JavaScript, images, or a generated site locally, the Node.js package http-server provides a small command-line server with minimal setup.

Run It Without a Global Install

If Node.js and npm are already installed, you can run the package with npx:

npx http-server .

The final . means “serve the current directory.” The command prints the local addresses and port it is using.

A global installation also works:

npm install -g http-server
http-server .

Using npx avoids requiring a machine-wide package installation for occasional use.

Choose a Port

npx http-server . -p 3000

Open the Browser Automatically

npx http-server . -o

Enable CORS Headers

For development scenarios that intentionally require cross-origin access:

npx http-server . --cors

Do not enable permissive CORS automatically in production; configure origins according to your application’s security requirements.

Disable or Adjust Caching During Development

http-server supports cache-related options. Check the installed version’s help for the exact flags:

npx http-server --help

This is preferable to relying on remembered flags because CLI options can evolve between versions.

Development Tool, Not a Production Architecture

http-server is convenient for local static-file testing and simple internal previews. A production deployment normally needs deliberate TLS, caching, compression, headers, logging, access controls, monitoring, and deployment practices supplied by a production web server or hosting platform.

Conclusion

Use http-server when you need a quick local HTTP origin instead of opening files directly from disk. npx http-server . is enough for most temporary development workflows, with optional flags for ports, browser opening, and CORS.

Related Posts

chevron-up