-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
84 lines (68 loc) · 2.01 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var express = require('express');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var exphbs = require('express-handlebars');
var winston = require('winston');
var fs = require('fs');
var Reference = require('./server/reference');
var controller = require('./server/controller');
var port = 8082;
// Load congig
GLOBAL.CONFIG = JSON.parse(fs.readFileSync('config.json' , 'utf8'));
//============= Create server ============
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(cookieParser());
//============= Static ============
app.use(express.static(__dirname + '/client'));
//============= Template engine ============
var hbs = exphbs.create({
defaultLayout: 'default',
});
app.engine('handlebars', hbs.engine);
app.set('view engine', 'handlebars');
// ============== ROUTES ================
app.get("/", function(req, res, next) {
if (req.query.ref) {
var ref = new Reference(req.query.ref);
res.locals = {
title: 'TR :: ' + ref.config.title,
config: JSON.stringify({
references: Reference.getAllList(),
ref: ref.config,
data: ref.data,
})
}
res.render('main', {layout: 'main'});
} else {
res.writeHead(502);
res.end('не задан параметр ref');
}
});
app.post("/", function(req, res, next) {
if (req.query.ref) {
var ref = new Reference(req.query.ref);
if (req.query.cmd) {
controller.process(ref, req.query.cmd, req.body, function(err, resp) {
if (err) {
res.writeHead(502);
res.end(err);
} else {
res.writeHead(200, {"Content-Type": "application/json"});
res.end(JSON.stringify(resp));
}
});
} else {
res.writeHead(502);
res.end('не задан параметр cmd');
}
} else {
res.writeHead(502);
res.end('не задан параметр ref');
}
});
var server = app.listen(port);
console.log('References started on port ' + port);