Skip to content

Instantly share code, notes, and snippets.

@Martlark
Forked from ryanflorence/static_server.js
Last active September 3, 2015 02:55
Show Gist options
  • Save Martlark/82f50a051ce5957ca451 to your computer and use it in GitHub Desktop.
Save Martlark/82f50a051ce5957ca451 to your computer and use it in GitHub Desktop.
Node.JS static file web server. Put it in your path to fire up servers in any directory, takes an optional port argument. With file change detection. Requirements: npm install websocket. To use. place in head of your html files <meta detection-change="true"> so that it will auto refresh if any served files change.
/*
node js static web server with file change detection
based upon
https://gist.github.com/ryanflorence/701407
by Andrew Rowe 2015.
requirements.
=============
npm install websocket
to use.
=======
place in head of your html files <meta detection-change="true"> so that it will auto
refresh if any served files change.
*/
var http = require("http"),
url = require("url"),
path = require("path"),
fs = require("fs"),
port = process.argv[2] || 8888;
var WebSocketServer = require('websocket').server;
var watchedInjectCode = "<script>\n" +
" var wSocket = new WebSocket('ws://localhost:"+port+"','refresh-protocol');\n" +
" wSocket.onmessage = function(evt) {\n" +
" console.log('refresh message from server');\n" +
" window.location.reload(1); \n" +
" }\n" +
" wSocket.onerror = function(evt) {\n" +
" console.log('wSocket error:'+evt);\n" +
" }\n" +
" </script>\n";
var watchedInjectCodeTag = '<meta detection-change="true">';
var watchedFiles = {};
var server = http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname
, filename = path.join(process.cwd(), uri);
fs.stat(filename, function(err,stat) {
currentFilename = null;
if(err) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
if (fs.statSync(filename).isDirectory()) filename += '/index.html';
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
// add listeners for served file changes
fs.stat( filename, function(err,stats){
if( !err ){
if( !watchedFiles.hasOwnProperty( filename ) ){
watchedFiles[filename] = fs.watchFile(filename, function(cur,prev){
if( cur.mtime.getTime() != prev.mtime.getTime() ){
console.log( 'change detected in: ' + filename );
for( var i in clients){
clients[i].sendUTF('refresh-protocol');
}
}
});
}
}
});
response.writeHead(200);
// inject the web socket code
file = file.replace(watchedInjectCodeTag,watchedInjectCode);
response.write(file, "binary");
response.end();
});
});
}).listen(parseInt(port, 10));
// http://codular.com/node-web-sockets
// add a web socket server to send change messages
wsServer = new WebSocketServer({
httpServer : server
});
var count = 0;
var clients = {};
wsServer.on('request',function(r){
var connection = r.accept('refresh-protocol', r.origin);
var id = count++;
clients[id] = connection;
//console.log((new Date()) + ' Connection accepted [' + id + ']');
connection.on('close', function(reasonCode, description) {
// console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.');
delete clients[id];
});
});
console.log("Static file server running at\n => http://localhost:" + port + "/\nCTRL + C to shutdown");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment