2024-06-22 14:08:05 -04:00
|
|
|
const express = require('express')
|
|
|
|
const router = express.Router()
|
|
|
|
const fs = require('fs/promises')
|
2024-06-22 08:09:40 -04:00
|
|
|
|
|
|
|
const file = './messages'
|
|
|
|
|
|
|
|
const get = (res, thankYou) => {
|
2024-06-22 14:08:05 -04:00
|
|
|
res.render('index', { title: 'Message Sage 😎', thankYou })
|
2024-06-22 08:09:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* GET home page. */
|
2024-06-22 14:08:05 -04:00
|
|
|
router.get('/', function (req, res, next) {
|
2024-06-22 08:09:40 -04:00
|
|
|
get(res, false)
|
2024-06-22 14:08:05 -04:00
|
|
|
})
|
2024-06-22 08:09:40 -04:00
|
|
|
|
|
|
|
router.post('/', (req, res) => {
|
|
|
|
const log = { message: req.body.message, timestamp: new Date() }
|
2024-06-22 14:08:05 -04:00
|
|
|
fs.appendFile(file, JSON.stringify(log) + '\n').catch(console.error)
|
2024-06-22 08:09:40 -04:00
|
|
|
|
|
|
|
get(res, true)
|
|
|
|
})
|
|
|
|
|
2024-06-22 14:08:05 -04:00
|
|
|
module.exports = router
|