-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
54 lines (43 loc) · 1.36 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
var express = require('express');
var routing = require('routing');
var adminRouting = require('adminRouting');
var app = express();
app.use('/', routing);
app.use('/admin', adminRouting);
/* Code below can be moved to modules. */
// Pattern matching.
app.get('/your*patt+ern(here)?', function(req, res) {
res.send('Matched the pattern.');
});
// Generate error.
app.get('/error', function(req, res) {
// something is undefined.
var error = 1 / something;
res.send('Code will never come here.');
});
// Handle 404 - Not Found.
app.use('*', function(req, res) {
res.status(404).send("Well, looks like we've got a four-o-four, boys! (That mean the page was not found)");
});
// Error handlers.
var logger = function(err, req, res, next) {
if (req.query.err == 'true') {
var error = 1 / somethingElse;
};
console.error(err.stack);
next();
};
var dbLogger = function(req, res, next) {
console.log('Error logged to DB.');
next();
};
var serveErrPage = function(req, res, next) {
res.status(500).send('Oops, looks like you broke something!');
};
app.use('/', logger, dbLogger, serveErrPage);
// Error handler for when the error handling goes awry!
app.use('/', function(err, req, res, next) {
// Everything failed...
res.status(500).send('Even the error handling is bugged...');
});
var server = app.listen(80, '127.0.0.1');