-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
76 lines (66 loc) · 2.52 KB
/
app.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
// const http = require('http');
// const https = require('https');
const express = require('express');
const fs = require('fs');
const url = require('url');
const template = require('./lib/template');
const db = require('./lib/mysql');
const app = express();
// const privateKey = fs.readFileSync('../../../Certbot/live/viclo.elitance.dev/privkey.pem', 'utf8');
// const certificate = fs.readFileSync('../../../Certbot/live/viclo.elitance.dev/cert.pem', 'utf8');
// const ca = fs.readFileSync('../../../Certbot/live/viclo.elitance.dev/chain.pem', 'utf8');
// const credentials = {
// key: privateKey,
// cert: certificate,
// ca,
// };
app.use(express.urlencoded({ extended: true }));
app.use(require('compression')());
app.use(require('cookie-parser')());
app.use(require('helmet')());
require('./lib/session')(app);
app.use(express.static('public'));
const passport = require('./lib/passport')(app);
app.use('/account', require('./router/account')(passport));
app.use('/docs', require('./router/docs'));
app.get('/', (req, res) => {
fs.readFile('html/index.html', 'utf8', (err, data) => {
res.send(data.replace('#$1', template.accLink(req.user)));
});
});
app.get('/search', (req, res) => {
const query = url.parse(req.url, true).query.q.toLowerCase();
let searchResults = new Array();
db.query('select * from docs', (err, docs) => {
docs.forEach((doc) => {
template.parse(doc.keywords).forEach((keyword) => {
if (keyword === query) searchResults.push([doc.id, doc.title, doc.content]);
});
});
if (!searchResults[0]) {
res.send(template.html(query, template.part('noResult', [query]), req));
} else {
if (searchResults.length > 1) {
res.send(template.html(query, `<div class='content'><h1>Search results for '${query}'</h1>${template.listData(searchResults)}</div>`, req));
} else {
res.redirect(`/docs/${searchResults[0][0]}`);
}
}
});
});
app.get('/contributors', (req, res) => {
res.send(template.html('Contributors', template.part('contrib'), req));
});
app.get('/contact', (req, res) => {
res.send(template.html('Contact', template.part('contact'), req));
});
app.use((req, res) => {
res.send(template.html('Not Found', template.part('notFound'), req));
});
app.listen(80, 'localhost', () => {
console.log('Development server running!');
});
// http.createServer(app).listen(80);
// https.createServer(credentials, app).listen(443, () => {
// console.log('Production server running!');
// });