-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Jeff Gold
committed
Jul 15, 2015
1 parent
83094b3
commit cbc9f5d
Showing
1 changed file
with
49 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,56 @@ | ||
#! /usr/bin/env node | ||
// Quick-and-dirty web server using Node.js | ||
// Republic Of Techies (RoT) group project. | ||
var sys = require('sys'); | ||
var http = require('http'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var port = 8080; | ||
(function() { | ||
'use strict'; | ||
|
||
http.createServer(function(request, response) { | ||
var url = request.url; | ||
if (url === '/') | ||
url = '/index.html'; | ||
var sys = require('sys'); | ||
var http = require('http'); | ||
var fs = require('fs'); | ||
var path = require('path'); | ||
var port = 8080; | ||
|
||
if (url === '/time') { | ||
response.writeHeader(200, { | ||
"Context-Type": "text/plain", | ||
"Access-Control-Allow-Origin": "http://localhost"}); | ||
response.write(new Date().toString()); | ||
var respond = function(response, code, headers, data) { | ||
response.writeHeader(code, headers); | ||
response.write(data); | ||
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(); | ||
}; | ||
|
||
http.createServer(function(request, response) { | ||
var url = request.url; | ||
if (url === '/') | ||
url = '/index.html'; | ||
|
||
if (url === '/time') { | ||
respond(response, 200, { | ||
"Context-Type": "text/plain", | ||
"Access-Control-Allow-Origin": "http://localhost"}, | ||
new Date().toString()); | ||
} | ||
}); | ||
}).listen(port); | ||
console.log('Server listening: http://localhost:' + port + '/'); | ||
|
||
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; | ||
respond(response, 404, { | ||
"Content-Type": "text/html"}, | ||
data.toString() | ||
.replace(/:PATH:/g, url)); | ||
}); | ||
} else throw err; | ||
} else { | ||
// :TODO: determine content type -- | ||
// by file extension? | ||
// by contents? | ||
// a combination of both? | ||
respond(response, 200, { | ||
"Content-Type": "text/html"}, data); | ||
} | ||
}); | ||
}).listen(port); | ||
console.log('Server listening: http://localhost:' + port + '/'); | ||
})(); |