-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
122 lines (115 loc) · 3.07 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const fs = require('fs')
const express = require('express')
const { createLogger, format, transports } = require('winston')
const APPNAME = 'ip-dir'
const FILEPATH = 'records.json'
const FIELDS = ['ip', 'name', 'description']
const PORT = 3000
const logger = createLogger({
level: 'info',
format: format.combine(
format.errors({ stack: true }),
format.timestamp(),
format.splat(),
format.json(),
),
transports: [
new transports.File({
filename: `${APPNAME}.log`
}),
new transports.Console({
format: format.combine(
format.colorize(),
format.printf(
({ level, message, timestamp }) => `${timestamp} ${level}: ${message}`
),
)
})
]
})
fs.exists(FILEPATH, (exists) => {
if (!exists) {
fs.writeFile(FILEPATH, JSON.stringify([]), () => {
logger.info(`Writing new file: ${FILEPATH}`)
})
}
})
const app = express()
app.use(express.urlencoded({ extended: true }))
app.get('/', (req, res) => {
fs.readFile(FILEPATH, (err, data) => {
if (err) {
logger.error(`Error reading file:\n${err}`)
}
res
.set('Content-Type', 'text/html')
.status(200)
.send(`
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${APPNAME}</title>
<style>
body {
font-family: monospace;
}
input {
margin-right: 2em;
}
table {
margin-top: 2em;
}
th {
padding-bottom: 0.5em;
}
th, td {
padding-right: 2em;
}
</style>
</head>
<body>
<div>
<form action="" method="POST">
${FIELDS.map(
(field) => `${field}:<input type="text" name="${field}">`).join('')
}
<input type="submit" value="Append">
</form>
<table>
<tr>
${FIELDS.map(
(field) => `<th align="left">${field}</th>`).join('')
}
</tr>
${JSON.parse(data).map(
(record) => `<tr>${FIELDS.map(
(field) => `<td>${field === 'ip' ?
`<a href="http://${record[field]}">${record[field]}</a>` :
record[field]
}</td>`).join('')
}</tr>`).join('\n')}
</table>
</div>
<script type="text/javascript">
</script>
</body>
</html>
`)})})
app.post('/', (req, res) => {
const record = Object.assign({}, ...FIELDS.map((field) => ({[field]: req.body[field]})))
fs.readFile(FILEPATH, (err, data) => {
var records = JSON.parse(data)
logger.info(`Appending new record:\n\t${JSON.stringify(record)}`)
records.push(record)
fs.writeFile(FILEPATH, JSON.stringify(records), (err) => {
if (err) {
logger.error(`Error writing file:\n${err}`)
}
res.redirect('back')
})
})
})
app.listen(PORT, () => {
logger.info(`${APPNAME} listening on port ${PORT}!`)
})