Skip to content

Instantly share code, notes, and snippets.

@rahul1346
Created February 17, 2015 00:48
Show Gist options
  • Save rahul1346/ee1d5511d67c054a00f2 to your computer and use it in GitHub Desktop.
Save rahul1346/ee1d5511d67c054a00f2 to your computer and use it in GitHub Desktop.
Vanilla node server
'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;
'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);
{
"name": "rest_framework",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}
'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;
'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;
'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