Created
February 17, 2015 00:48
-
-
Save rahul1346/ee1d5511d67c054a00f2 to your computer and use it in GitHub Desktop.
Vanilla 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
'use strict'; | |
var http = require("http"); | |
var url = require("url"); | |
function start(route, handle) { | |
function onRequest(request, response) { | |
if (typeof(handle[request.url]) === 'function') { | |
var pathname = url.parse(request.url).pathname; | |
console.log("Request for " + pathname + " received."); | |
route(handle, pathname, response, request); | |
} else { | |
response.writeHead(404, { | |
'Content-Type': 'application/json' | |
}); | |
response.write(JSON.stringify({msg: 'page not found'})); | |
response.end(); | |
} | |
} | |
http.createServer(onRequest).listen(8888); | |
console.log("Server has started."); | |
} | |
exports.start = start; |
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
'use strict'; | |
var server = require("./http_server"); | |
var router = require("./router"); | |
var requestHandlers = require("./requestHandler"); | |
var handle = {}; | |
handle["/"] = requestHandlers.start; | |
handle["/start"] = requestHandlers.start; | |
handle["/upload"] = requestHandlers.upload; | |
handle["/show"] = requestHandlers.show; | |
handle["/update"] = requestHandlers.update; | |
server.start(router.route, handle); |
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
{ | |
"name": "rest_framework", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC" | |
} |
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
'use strict'; | |
var querystring = require("querystring"); | |
function start(response, postData) { | |
console.log("Request handler 'start' was called."); | |
var body = '<html>'+ | |
'<head>'+ | |
'<meta http-equiv="Content-Type" content="text/html; '+ | |
'charset=UTF-8" />'+ | |
'</head>'+ | |
'<body>'+ | |
'<form action="/upload" method="post">'+ | |
'<textarea name="text" rows="20" cols="60"></textarea>'+ | |
'<input type="submit" value="Submit text" />'+ | |
'</form>'+ | |
'</body>'+ | |
'</html>'; | |
response.writeHead(200, {"Content-Type": "text/html"}); | |
response.write(body); | |
response.end(); | |
} | |
function upload(response, postData) { | |
console.log("Request handler 'upload' was called."); | |
response.writeHead(200, {"Content-Type": "text/plain"}); | |
response.write("You've sent the text: "+ | |
querystring.parse(postData).text); | |
response.end(); | |
} | |
exports.start = start; | |
exports.upload = upload; |
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
'use strict'; | |
//var querystring = require("querystring"), | |
var fs = require("fs"), | |
formidable = require("formidable"), | |
util = require('util'), | |
jsonObject = null, | |
jsonFile = null; | |
function start(response) { | |
console.log("Request handler 'start' was called."); | |
var body = '<html>'+ | |
'<head>'+ | |
'<meta http-equiv="Content-Type" '+ | |
'content="text/html; charset=UTF-8" />'+ | |
'</head>'+ | |
'<body>'+ | |
'<form action="/upload" enctype="multipart/form-data" '+ | |
'method="post">'+ | |
'<input type="file" name="upload" multiple="multiple">'+ | |
'<input type="submit" value="Upload file" />'+ | |
'</form>'+ | |
'</body>'+ | |
'</html>'; | |
response.writeHead(200, {"Content-Type": "text/html"}); | |
response.write(body); | |
response.end(); | |
} | |
function upload(response, request) { | |
console.log("Request handler 'upload' was called."); | |
var form = new formidable.IncomingForm(); | |
console.log("about to parse"); | |
form.parse(request, function(error, fields, files){ | |
response.writeHead(200, {'content-type': 'application/json'}); | |
response.write('received upload:\n\n'); | |
response.end(util.inspect({fields: fields, files: files})); | |
}); | |
form.on('file', function(field, file) { | |
console.log(jsonFile); | |
if (file.type == 'application/json') { | |
jsonObject = JSON.parse(fs.readFileSync(file.path, 'utf8')); | |
console.log(jsonObject); | |
jsonFile = file.path; | |
} else { console.log('wrong file');} | |
}); | |
} | |
function update(response, request) { | |
console.log("Request handler 'update' was called."); | |
response.writeHead(200, {"Content-Type": "application/json"}); | |
var json = JSON.parse(fs.readFileSync('./tmp/obj.json')); | |
json.newValue = 'testVal'; | |
fs.writeFileSync("./tmp/obj.json", JSON.stringify(json)); | |
} | |
function show(response) { | |
console.log("Request handler 'show' was called."); | |
response.writeHead(200, {"Content-Type": "application/json"}); | |
fs.createReadStream("./tmp/obj.json").pipe(response); | |
} | |
exports.start = start; | |
exports.update = update; | |
exports.upload = upload; | |
exports.show = show; |
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
'use strict'; | |
function route(handle, pathname, response, request) { | |
console.log("About to route a request for " + pathname); | |
if (typeof handle[pathname] === 'function') { | |
handle[pathname](response, request); | |
}else{ | |
console.log("No request handler found for " + pathname); | |
response.writeHead(404, {"Content-Type": "text/plain"}); | |
response.write("404 Not found"); | |
response.end(); | |
} | |
} | |
exports.route = route; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment