forked from yarrumk/coffeecoffeecoffee
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
121 lines (101 loc) · 3.47 KB
/
Copy pathserver.js
File metadata and controls
121 lines (101 loc) · 3.47 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// All of our requirements to make the app work
var config = require('./etc/config');
var path = require('path');
var express = require('express');
var bodyParser = require('body-parser');
var session = require('cookie-session');
var cookieParser = require('cookie-parser');
var compression = require('compression');
var morgan = require('morgan');
var mustache = require('mustache');
var mustacheExpress = require('mustache-express');
var app = express();
var cms = require('./routes/cms');
var featureserver = require('./routes/featureserver');
// Enable or disable logging of http requests.
app.use(morgan('combined', {
skip: function (req, res) { return config.server.disable_log; }
}));
// Register '.html' extension with The Mustache Express
app.engine('html', mustacheExpress());
app.set('view engine', 'mustache');
app.set('views', __dirname + '/views');
// Serve static content
app.use(express.static(path.join(__dirname, 'public')));
// for parsing application/json
app.use(bodyParser.json());
// for parsing application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
// Handle session cookies
app.use(session({ secret: config.cookies.secret, cookie: { maxAge: 60000 }}));
app.use(cookieParser( config.cookies.secret ));
// Compress responses greater than 512 Bytes
app.use(compression({
threshold: 512
}));
// Our request routers.
//
app.use('/cms', cms);
app.use('/fs', featureserver);
// Setup our error handlers
app.use(logErrors);
app.use(clientErrorHandler);
app.use(errorHandler);
// Do we show stack traces on the console?
function logErrors(err, req, res, next) {
if(!config.server.disable_log){
console.error(err.stack);
}
next(err);
}
// If this is an AJAX request, send an error message in JSON format.
function clientErrorHandler(err, req, res, next) {
if (req.xhr) {
res.status(500).send({ error: err.message });
} else {
next(err);
}
}
// If we get this far, it's a website error so the user needs an error page.
function errorHandler(err, req, res, next) {
res.status(500);
if(!config.server.disable_log){
console.log(err);
}
var info = { message: "Error", stack: "", layout: false };
if (err instanceof Error) {
info.message = err.message;
if(config.server.devmode){
info.stack = err.stack && err.stack.split('\n').join('<br/>');
}
}
res.render('error.html', info);
}
// Handle 404 pages and 404 AJAX requests.
app.use(function(req, res, next){
res.status(404);
// respond with 404 page if we can't find something.
if (req.accepts('html')) {
res.render('404.html', { url: req.url });
return;
}
// respond with json if this is an ajax request
if (req.accepts('json')) {
res.send({ error: 'Not found' });
return;
}
// default to plain-text if we can't figure out
// what the client accepts.
res.type('txt').send('Not found');
});
// Set the port number we are going to use.
app.set('port', (process.env.PORT || config.server.port || 5000));
// Finally, set up a server instance to accept requests.
var server = app.listen(app.get('port'), function() {
console.log( '---\n');
console.log( config.server.version_str + ' listening on port ' + server.address().port);
if(config.server.devmode){
console.log( 'Running in Development mode.\n');
console.log( '---');
}
});