-
Notifications
You must be signed in to change notification settings - Fork 18
/
app.js
66 lines (55 loc) · 2 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
/*-----------------------------------------------------------------------------------------------
* Copyright (c) 2019 Red Hat, Inc. and others
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*
* SPDX-License-Identifier: MIT
*-----------------------------------------------------------------------------------------------
*/
const express = require('express')
const path = require('path');
const app = express();
const bodyParser = require('body-parser')
const messages = require('./routes/messages')
const index = require('./routes/index')
const util = require('./utils')
// Connect to MongoDB, will retry only once
messages.connectToMongoDB()
app.set("view engine", "pug")
app.set("views", path.join(__dirname, "views"))
const router = express.Router()
app.use(router)
app.use(express.static('public'))
router.use(bodyParser.urlencoded({ extended: false }))
// Starts an http server on the 8080 port
const PORT = 8080;
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
console.log('Press Ctrl+C to quit.');
});
// Handles GET request to /
router.get("/", async (req, res) => {
// retrieve list of messages from the db, and use them to render the HTML template
let savedMessages = await index.getMessages();
console.log(`Read all mesages`)
const result = util.formatMessages(savedMessages)
res.render("home", { messages: result })
})
// Handles POST request to /post
router.post('/post', (req, res) => {
console.log(`received request: ${req.method} ${req.url}`)
// validate request
const name = req.body.name
const message = req.body.message
if (!name || name.length == 0) {
res.status(400).send("name is not specified")
return
}
if (!message || message.length == 0) {
res.status(400).send("message is not specified")
return
}
// send the new message to the db and redirect to the homepage
console.log(`posting to db: name: ${name} body: ${message}`)
index.setMessage(name, message)
res.redirect('/')
});