forked from coveo/search-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
68 lines (59 loc) · 2.04 KB
/
index.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
// This file is used for heroku deploy used on pull requests
const express = require('express');
const app = express();
const { Client } = require('pg');
const bodyParser = require('body-parser');
app.use(
bodyParser.text({
type: 'text/html'
})
);
const client = new Client({
connectionString: process.env.DATABASE_URL,
user: 'olamothe',
database: 'qa'
});
client.connect();
app.set('port', process.env.PORT || 8080);
app.use('/', express.static(__dirname + '/bin'));
app.get('/qa/:pagename', function(request, response) {
client.query(`SELECT content FROM pages WHERE name LIKE $1 LIMIT 1;`, [request.params.pagename], (err, res) => {
if (err) {
response.status(500).send(err.message);
} else if (res.rowCount == 1) {
return response.send(res.rows[0].content);
} else {
return response.sendStatus(404);
}
});
});
app.post('/qa/:pagename', function(request, response) {
if (request.body && request.headers.authentication == `Bearer ${process.env.UPLOAD_KEY}`) {
client.query(`SELECT 1 FROM pages WHERE name LIKE $1;`, [request.params.pagename], (err, res) => {
if (err) {
response.status(500).send(err.message);
} else if (res.rowCount == 1) {
client.query(`UPDATE pages SET content=$1 WHERE name LIKE $2;`, [request.body, request.params.pagename], (err, res) => {
response.sendStatus(200);
});
} else {
client.query(`INSERT INTO pages (content, name) VALUES ($1, $2);`, [request.body, request.params.pagename], (err, res) => {
response.sendStatus(200);
});
}
});
} else {
response.sendStatus(403);
}
});
app.get('/', function(request, response) {
console.log('accessing ' + __dirname + '/bin/Index.html');
response.sendFile(__dirname + '/bin/Index.html');
});
app.get(/^(.+)$/, function(req, res) {
console.log('accessing ' + __dirname + req.params[0]);
res.sendFile(__dirname + req.params[0]);
});
app.listen(app.get('port'), function() {
console.log('Node app is running at localhost:' + app.get('port'));
});