-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
61 lines (53 loc) · 1.6 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
55
56
57
58
59
60
61
var express = require('express'),
winston = require('winston'),
fs = require('fs');
var app = express();
// Winston configuration
winston.add(winston.transports.File, { filename: 'debug-exceptions.log' });
winston.handleExceptions(new winston.transports.File({ filename: 'exceptions.log' }));
app.configure(function() {
var oneDay = 86400000;
app.use(express.compress());
app.use(express.static(__dirname), { maxAge: oneDay });
});
app.get('/section', function(req, res, next) {
var sectionFile = req.query.name + '.html';
fs.readFile(sectionFile, { encoding: 'utf8' }, function(err, data) {
if(err) next(err);
if(data) {
res.send(data);
} else {
next(new Error('No section file found'));
}
});
});
app.get('/sectionData', function(req, res, next) {
var sectionDataFile = 'data/' + req.query.name + '.json';
fs.readFile(sectionDataFile, { encoding: 'utf8' }, function(err, data) {
if(err) next(err);
if(data) {
res.set('Content-Type', 'application/json');
res.send(data);
} else {
next(new Error('No section data file found'));
}
});
});
app.get('/availableItems', function(req, res, next) {
var availableItemsFile = 'data/availableItems.json';
fs.readFile(availableItemsFile, { encoding: 'utf8' }, function(err, data) {
if(err) next(err);
if(data) {
res.set('Content-Type', 'application/json');
res.send(data);
} else {
next(new Error('No available items data found'));
}
});
});
app.use(function(err, req, res, next){
winston.error('Exception happended!', err);
});
app.listen(80);
winston.info('Server started on port 80');
winston.info('Press Ctrl+C to exit');