Skip to content

Instantly share code, notes, and snippets.

Created January 8, 2013 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/11716a499d5c294b9b19 to your computer and use it in GitHub Desktop.
Save anonymous/11716a499d5c294b9b19 to your computer and use it in GitHub Desktop.
My simple server
// TodoServer.js
'use strict';
var http = require("http"),
https = require("https"),
url = require("url"),
path = require("path"),
fs = require("fs"),
os = require("os"),
sys = require('sys'),
mongojs = require("mongojs");
var port = process.argv[2] || 8080,
restUrl = "/api/1/databases/",
restHost = "api.mongolab.com",
dbUrl = "yodertv:sugmag@ds045907.mongolab.com:45907/test-todo", // "username:password@example.com/mydb"
db = mongojs.connect(dbUrl);
http.createServer(function(req, response) {
var uri = url.parse(req.url).pathname;
sys.log(req.connection.remoteAddress + ": " + req.method + " " + req.url);
if (uri.indexOf(restUrl) == 0) { // Proxy API calls to Mongolab
var options = {
hostname: restHost,
port: 443,
path: req.url,
method: req.method,
headers: req.headers
};
var proxy = https.request( options, function (res) {
// console.log('STATUS: ' + res.statusCode);
// console.log('HEADERS: ' + JSON.stringify(res.headers));
proxy.on('error', function(e) {
console.log("Error:", e);
});
res.on('data', function(chunk) {
// sys.log(chunk);
response.write(chunk, 'binary');
});
res.on('end', function() {
// sys.log("End");
response.end();
});
response.setHeader('allow', ['PUT', 'OPTIONS', 'GET', 'POST']);
response.writeHead(res.statusCode, res.headers);
//response.writeHead(200, "OK", {'Content-Type': 'text/html'});
//response.end();
});
req.on('data', function(chunk) {
proxy.write(chunk, 'binary');
});
req.on('end', function() {
sys.log('Proxy end')
proxy.end();
});
}
else if (req.method == 'DELETE') { // Delete archive collection using mongojs.
var name = uri.substr(1, uri.length);
var toDelete = db.collection(name);
toDelete.drop();
req.on('end', function() {
// empty 200 OK response for now
response.writeHead(200, "OK", {'Content-Type': 'text/html'});
response.end();
});
console.log(req.method +" to " + name);
}
else { // default static file server for html and script files.
var filename = path.join(process.cwd(), uri);
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("todoServer running at http://" + os.hostname() + ":" + port + "/\nCTRL + C to shutdown");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment