Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node http-server not serving updated html files

I am building out a front-end web app with angular (mostly ui-router) and doing local development by serving the html files through node http-server. I have noticed that http-server isn't serving my static html files when I make updates, which is challenging to my local development.

I have http-server installed globally with npm install http-server -g and start it up by going to the root project folder and running http-server. It defaults to localhost:8080- the two ways that seem to work is changing the port number after each update or going through chrome incognito mode.

Is there a way to use http-server normally without having to change the port or using incognito mode?

If it is relevant, I am using MBP v. 10.11.3

Thank you!

like image 332
wariofan1 Avatar asked Jul 10 '16 18:07

wariofan1


People also ask

How do I reload HTTP server?

You can use the service or systemctl command to restart httpd server. Another option is use /etc/init. d/httpd service script under Linux or Unix-like systems. This page explains how to reload or restart HTTPD web serer using the command-line option.

CAN node serve HTML?

The open source text editor Brackets also includes a NodeJS static web server. Just open any HTML file in Brackets, press "Live Preview" and it starts a static server and opens your browser at the page. The browser will auto refresh whenever you edit and save the HTML file.

What does createServer do in node JS?

createServer() method turns your computer into an HTTP server. The http. createServer() method creates an HTTP Server object. The HTTP Server object can listen to ports on your computer and execute a function, a requestListener, each time a request is made.


1 Answers

the two ways that seem to work is changing the port number after each update or going through chrome incognito mode.

Your problem is client-side caching. Incognito mode has its own data directory, independent from your normal browsing.

Fortunately, http-server provides a way for you to set the cache control headers.

-c Set cache time (in seconds) for cache-control max-age header, e.g. -c10 for 10 seconds (defaults to '3600'). To disable caching, use -c-1 (and possibly -p 8080 to force the server to clear the cache for that port; can be removed on subsequent runs).

It's listed in the documentation here: https://github.com/indexzero/http-server

You can read up on HTTP caching directives here: https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching?hl=en

like image 197
Brad Avatar answered Oct 03 '22 20:10

Brad