-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
28 lines (21 loc) · 766 Bytes
/
Copy pathapp.js
File metadata and controls
28 lines (21 loc) · 766 Bytes
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
const express = require('express')
const path = require('path')
const fs = require('fs').promises
const app = express()
app.set('view engine', 'ejs')
app.set('views', path.resolve(__dirname, 'pages'))
app.use(express.static('public'))
app.use(express.urlencoded({ extended: true }))
const port = process.env.PORT ?? 3000
const logsPath = path.resolve(__dirname, 'data', 'logs.txt')
app.get('/', async (req, res) => {
const data = await fs.readFile(logsPath, 'utf-8')
const logs = data.split('\r\n').filter(i => !!i)
res.render('index', {logs})
})
app.post('/', async (req, res) => {
const text = req.body.text
await fs.appendFile(logsPath, `${text}\r\n`)
res.redirect('/')
})
app.listen(port, () => console.log(`Server listening on port ${port}`))