"main": "index.js", | "main": "index.js", | ||||
"scripts": { | "scripts": { | ||||
"test": "echo \"Error: no test specified\" && exit 1", | "test": "echo \"Error: no test specified\" && exit 1", | ||||
"start": "node index.js", | |||||
"start": "node server.js", | |||||
"setup": "node scripts/setup.js", | "setup": "node scripts/setup.js", | ||||
"reset": "node scripts/reset.js" | "reset": "node scripts/reset.js" | ||||
}, | }, |
var http = require("http"); | var http = require("http"); | ||||
var https = require("https"); | var https = require("https"); | ||||
var fs = require("fs"); | var fs = require("fs"); | ||||
var domain = require("domain"); | |||||
var loader = require("./lib/loader.js"); | var loader = require("./lib/loader.js"); | ||||
var pg = require("pg"); | var pg = require("pg"); | ||||
var Context = require("./lib/context.js"); | var Context = require("./lib/context.js"); | ||||
"/favicon.ico": "favicon.ico", | "/favicon.ico": "favicon.ico", | ||||
"/global.css": "global.css", | "/global.css": "global.css", | ||||
"/global.js": "global.js", | "/global.js": "global.js", | ||||
"/404": "404.html", | |||||
"/404": "404.node.js", | |||||
//Index files | //Index files | ||||
"/": "index/index.node.js", | "/": "index/index.node.js", | ||||
console.log("Listening on port "+conf.port+"."); | console.log("Listening on port "+conf.port+"."); | ||||
}); | }); | ||||
//We don't want to crash even if something throws an uncaught exception. | |||||
var d = domain.create(); | |||||
d.on("error", function(err) { | |||||
console.trace(err); | |||||
}); | |||||
process.on("uncaughtException", function(err) { | |||||
console.trace(err); | |||||
}); |
<!DOCTYPE html> | |||||
<html> | |||||
<head> | |||||
<meta charset="utf-8"> | |||||
</head> | |||||
<body> | |||||
404, file not found. | |||||
</body> | |||||
</html> |
module.exports = function(ctx) { | |||||
ctx.end(ctx.view("404")); | |||||
} |
var fs = require("fs"); | var fs = require("fs"); | ||||
module.exports = function(ctx) { | module.exports = function(ctx) { | ||||
var id = ctx.req.url.split("?")[1] | |||||
.replace(/\..*/, ""); | |||||
var id; | |||||
try { | |||||
id = ctx.req.url.split("?")[1].replace(/\..*/, ""); | |||||
} catch (err) { | |||||
return ctx.end(ctx.view("404")); | |||||
} | |||||
var readStream = fs.createReadStream(ctx.conf.dir.imgs+"/"+id); | var readStream = fs.createReadStream(ctx.conf.dir.imgs+"/"+id); | ||||
readStream.pipe(ctx.res); | readStream.pipe(ctx.res); | ||||
readStream.on("error", function(err){ | |||||
if (err.code == "ENOENT") | |||||
ctx.end(ctx.view("404")); | |||||
else | |||||
ctx.end(err.toString()); | |||||
}); | |||||
} | } |
module.exports = function(ctx) { | module.exports = function(ctx) { | ||||
var id = parseInt(ctx.req.url.split("?")[1]); | var id = parseInt(ctx.req.url.split("?")[1]); | ||||
if (isNaN(id)) | |||||
return ctx.end(ctx.view("404")); | |||||
ctx.db.query( | ctx.db.query( | ||||
"SELECT id, name, description, extension "+ | "SELECT id, name, description, extension "+ | ||||
"FROM images "+ | "FROM images "+ |