Browse Source

less crashes, better error handling

master
mort 8 years ago
parent
commit
8f16208e00
6 changed files with 40 additions and 4 deletions
  1. 1
    1
      package.json
  2. 11
    1
      server.js
  3. 9
    0
      views/404.html
  4. 3
    0
      web/404.node.js
  5. 13
    2
      web/i/index.node.js
  6. 3
    0
      web/view/index.node.js

+ 1
- 1
package.json View File

@@ -5,7 +5,7 @@
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node index.js",
"start": "node server.js",
"setup": "node scripts/setup.js",
"reset": "node scripts/reset.js"
},

index.js → server.js View File

@@ -1,6 +1,7 @@
var http = require("http");
var https = require("https");
var fs = require("fs");
var domain = require("domain");
var loader = require("./lib/loader.js");
var pg = require("pg");
var Context = require("./lib/context.js");
@@ -13,7 +14,7 @@ var endpoints = {
"/favicon.ico": "favicon.ico",
"/global.css": "global.css",
"/global.js": "global.js",
"/404": "404.html",
"/404": "404.node.js",

//Index files
"/": "index/index.node.js",
@@ -77,3 +78,12 @@ db.connect(function() {

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);
});

+ 9
- 0
views/404.html View File

@@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
404, file not found.
</body>
</html>

+ 3
- 0
web/404.node.js View File

@@ -0,0 +1,3 @@
module.exports = function(ctx) {
ctx.end(ctx.view("404"));
}

+ 13
- 2
web/i/index.node.js View File

@@ -1,9 +1,20 @@
var fs = require("fs");

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);
readStream.pipe(ctx.res);

readStream.on("error", function(err){
if (err.code == "ENOENT")
ctx.end(ctx.view("404"));
else
ctx.end(err.toString());
});
}

+ 3
- 0
web/view/index.node.js View File

@@ -1,6 +1,9 @@
module.exports = function(ctx) {
var id = parseInt(ctx.req.url.split("?")[1]);

if (isNaN(id))
return ctx.end(ctx.view("404"));

ctx.db.query(
"SELECT id, name, description, extension "+
"FROM images "+

Loading…
Cancel
Save