Created
October 21, 2014 23:39
-
-
Save mboynes/ae222cfa2bb9b8ff6c30 to your computer and use it in GitHub Desktop.
Simple node server
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
var http = require("http") | |
, url = require("url") | |
, path = require("path") | |
, fs = require("fs") | |
, qs = require('querystring') | |
, tasks = {} | |
, port = process.argv[2] || 8888 | |
, next_id = 1 | |
; | |
// Create a basic server | |
http.createServer(function(request, response) { | |
var uri = url.parse(request.url).pathname | |
, filename = path.join(process.cwd(), uri) | |
, id | |
; | |
// Log the request | |
console.log( request.method + " " + uri ); | |
// If this is a request to /tasks/#, let's parse the number and rewrite the uri | |
if ( match = /tasks\/(\d+)/.exec( uri ) ) { | |
uri = '/tasks'; | |
id = match[1]; | |
if ( typeof tasks[ id ] === 'undefined' ) { | |
response.writeHead( 404, { "Content-Type": "application/json" } ); | |
response.end(); | |
return; | |
} | |
} | |
switch ( uri ) { | |
case '/tasks' : | |
// regardless we'll be sending JSON, and by now we'll assume it's 200 or 201 | |
response.writeHead( | |
( 'POST' === request.method ? 201 : 200 ), | |
{"Content-Type": "application/json"} | |
); | |
// read the body of the request if applicable | |
var body = ''; | |
request.on('data', function(data) { | |
body += data; | |
}); | |
// If this is a POST, we're save a new task, so we need a new id | |
if ( 'POST' === request.method ) { | |
id = next_id++; | |
} | |
switch ( request.method ) { | |
case 'POST': | |
case 'PUT': | |
request.on('end', function() { | |
var post = JSON.parse( body ); | |
tasks[ id ] = { | |
id: id, | |
title: post.title || '', | |
due: post.date || 'today', | |
complete: ( typeof post.complete === 'undefined' || post.complete === 'true' ? false : post.complete ) | |
}; | |
response.end( JSON.stringify( tasks[ id ] ) ); | |
}); | |
break; | |
case 'DELETE': | |
delete tasks[ id ]; | |
response.end(); | |
break; | |
default: | |
response.end( JSON.stringify( tasks ) ); | |
} | |
break; | |
// If we're not dealing with a /tasks url, we'll load static files. Simple, no? | |
default : | |
fs.exists(filename, function(exists) { | |
if(!exists) { | |
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; | |
} | |
response.writeHead(200); | |
response.write(file, "binary"); | |
response.end(); | |
}); | |
}); | |
} | |
}).listen(parseInt(port, 10)); | |
console.log( "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