-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
114 lines (91 loc) · 2.7 KB
/
Copy pathindex.js
File metadata and controls
114 lines (91 loc) · 2.7 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
/* jshint node: true */
'use strict';
process.chdir(__dirname);
const express = require('express');
const exphbs = require('express-handlebars');
const bodyParser = require('body-parser');
const _ = require('underscore');
const dbQuery = require('./src/server/db-query');
const apiQuery = require('./src/server/api-query');
const favicon = require('serve-favicon');
const app = express();
// App configuration
const hbs = exphbs.create({
helpers: {
section: (name, options) => {
if (!this._sections) this._sections = {};
this._sections[name] = options.fn(this);
return null;
}
}
});
app.engine('hbs', exphbs.engine({defaultLayout: 'fi_main', extname: '.hbs'}));
app.set('view engine', 'hbs');
app.set('port', process.env.HTTP_PORT);
app.use(express.static(__dirname + '/public'));
app.use(favicon(__dirname + '/public/favicon.ico'));
app.enable('trust proxy', process.env.ENABLE_PROXY);
// Middleware for handling the queries submitted using the POST method
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// Process the query
app.post('/:language/process', (req, res, next) => {
const language = req.params.language
const texts = {
'fi': 'Ei hakutuloksia',
'sv': 'Inga sökresultat',
'en': 'Nothing was found'
}
dbQuery(req, doc => {
if (!doc) {
const err = new Error("Unsupported Media Type");
err.status = 415;
return next(err);
} else if (doc.length === 0) {
res.render('empty', { layout: language + '_main' , body: texts[language] });
} else {
res.render(language + '_results', { layout: language + '_main' , results: doc });
}
});
});
// Root
app.get('/index/:language/', (req, res) => {
const language = req.params.language
res.render(language + '_home', { layout: language + '_main' });
});
app.get('/accessibility/:language/', (req, res) => {
const language = req.params.language
res.render(language + '_accessibility', { layout: 'container_' + language });
});
// REST api
app.get('/api/query', (req, res) => {
apiQuery(req, res);
});
// Api page
app.get('/api/:language/', (req, res) => {
const language = req.params.language
res.render(language + '_api', { layout: language + '_main' });
});
app.get('/', (req, res) => {
res.status(302);
res.redirect('/index/fi');
});
app.use('/', (req, res) => {
res.status(404);
res.render('404');
});
app.use( (err, req, res, next) => {
res.status(415);
res.render('415');
});
// 500 error handler (middleware)
app.use( (err, req, res) => {
res.status(500);
res.render('500');
});
app.listen(app.get('port'), () => {
console.log( 'Express started on http://localhost:' +
app.get('port') + '; press Ctrl-C to terminate.' );
});