-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAPI.js
78 lines (72 loc) · 2.13 KB
/
API.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
const express = require('express')
const bodyParser = require('body-parser')
const path = require('path')
const fs = require('fs')
const app = express()
app.use(bodyParser.urlencoded({ extended: false }))
app.use(bodyParser.json())
app.set('view engine', 'ejs')
app.get('/read', function (req, res) {
const express = require('express')
app.use(express.static(path.join(__dirname)))
res.sendFile(path.join(__dirname, './', 'data.txt'))
})
app.post('/write/:data', function (request, respond) {
let stringToAppend = request.params.data
const filePath = 'data.txt'
stringToAppend += '\n'
fs.appendFile(filePath, stringToAppend, function () {
respond.end('Ended!')
})
})
app.put('/update/:linenumber', function (req, res) {
const linenumber = req.params.linenumber
const filePath = 'data.txt'
let dataArray = []
console.log(linenumber)
console.log(req.body.value)
fs.readFile(filePath, function (err, data) {
if (err) {
console.log(err)
} else {
dataArray = data.toString().split('\n')
console.log(dataArray.length)
if (linenumber <= dataArray.length) {
dataArray[linenumber] = req.body.value
const stringToWrite = dataArray.join('\n').toString()
console.log(dataArray)
fs.writeFileSync(filePath, stringToWrite)
res.end('Done!')
} else {
res.sendStatus(500)
}
}
})
})
app.delete('/destroy/:linenumber', function (req, res) {
const linenumber = req.params.linenumber
const filePath = 'data.txt'
console.log(linenumber)
console.log(req.body.value)
fs.readFile(filePath, function (err, data) {
if (err) {
console.log(err)
} else {
let dataArray = []
dataArray = data.toString().split('\n')
console.log(dataArray.length)
if (linenumber <= dataArray.length) {
dataArray.splice(linenumber, 1)
const stringToWrite = dataArray.join('\n').toString()
console.log(dataArray)
fs.writeFileSync(filePath, stringToWrite)
res.end('Done!')
} else {
res.sendStatus(500)
}
}
})
})
app.listen(3000, function () {
console.log('Started on PORT 3000')
})