diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/index.html b/index.html new file mode 100644 index 0000000..dcc3ece --- /dev/null +++ b/index.html @@ -0,0 +1,4 @@ + +
This page is being served from a RaspberryPi
diff --git a/page404.html b/page404.html new file mode 100644 index 0000000..461fda5 --- /dev/null +++ b/page404.html @@ -0,0 +1,4 @@ + +Sorry, but the page requested is not available on this server.
diff --git a/server.js b/server.js new file mode 100755 index 0000000..5e59b2b --- /dev/null +++ b/server.js @@ -0,0 +1,46 @@ +#! /usr/bin/env node +// Quick-and-dirty web server using Node.js +var sys = require('sys'); +var http = require('http'); +var fs = require('fs'); +var path = require('path'); +var port = 8080; + +http.createServer(function(request, response) { + var url = request.url; + if (url === '/') + url = '/index.html'; + + if (url === '/time') { + response.writeHeader(200, { + "Context-Type": "text/plain", + "Access-Control-Allow-Origin": "http://localhost/"}); + response.write(new Date().toString()); + response.end(); + } + + fs.readFile(path.join('.', url), function (err, data) { + if (err) { + if (err.code === 'ENOENT') { + fs.readFile('page404.html', function(err, data) { + if (err) + throw err; + response.writeHeader(404, { + "Content-Type": "text/html"}); + response.write(data.toString() + .replace(/:PATH:/g, url)); + response.end(); + }); + } else throw err; + } else { + // :TODO: determine content type -- + // by file extension? + // by contents? + // a combination of both? + response.writeHeader(200, {"Content-Type": "text/html"}); + response.write(data); + response.end(); + } + }); +}).listen(port); +console.log('Server listening: http://localhost:' + port + '/');