Created
November 4, 2012 20:00
-
-
Save StevenMcD/4013389 to your computer and use it in GitHub Desktop.
NodeJS - HTML version of basic "todo" server
This file contains hidden or 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'), | |
qs = require('querystring'), | |
items = []; | |
var server = http.createServer(function(req, res){ | |
if('/' == req.url){ | |
switch(req.method){ | |
case 'GET': | |
show(res); | |
break; | |
case 'POST': | |
add(req, res); | |
break; | |
default: | |
badRequest(res); | |
} | |
} else { | |
notFound(res); | |
} | |
}).listen(3000); | |
function show(res) { | |
var html = '<h1>Todo List</h1>' | |
+ '<ul>' | |
+ items.map(function(item){ | |
return '<li>' + item + '</li>' | |
}).join('') | |
+ '</ul>' | |
+ '<form method="post" action="/">' | |
+ '<p><input type="text" name="item" /></p>' | |
+ '<p><input type="submit" value="Add Item" /></p>' | |
+ '</form>'; | |
res.setHeader('Content-Type', 'text/html'); | |
res.setHeader('Content-Length', Buffer.byteLength(html)); | |
res.end(html); | |
}; | |
function notFound(res) { | |
res.statusCode = 404; | |
res.setHeader('Content-Type', 'text/plain'); | |
res.end('Not Found'); | |
}; | |
function badRequest(res) { | |
res.statusCode = 400; | |
res.setHeader('Content-Type', 'text/plain'); | |
res.end('Bad Request'); | |
}; | |
function add(req, res){ | |
var body = ''; | |
req.setEncoding('utf8'); | |
req.on('data', function(chunk){body += chunk}); | |
req.on('end', function(){ | |
var obj = qs.parse(body); | |
items.push(obj.item); | |
show(res); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment