message-me/routes/index.js

24 lines
533 B
JavaScript

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