-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
33 lines (30 loc) · 811 Bytes
/
server.js
File metadata and controls
33 lines (30 loc) · 811 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
29
30
31
32
33
const express = require('express');
const watch = require('fs').watch;
const read = require('read-last-lines').read;
const app = express();
const filePath = './text.txt';
app.get('/:noOfLines?', (request, response) => {
const {
params: {
noOfLines = 10,
} = {},
} = request;
read(filePath, noOfLines)
.then(lines => { response.send(lines); })
.catch((error) => { console.log('Error:', error); });
watch(filePath, (event, filename) => {
if (filename && event ==='change') {
read(filePath, noOfLines)
.then(lines => { response.send(lines); })
.catch((error) => console.log('Error:', error));
} else {
response.send('Error: Unable to access the file');
}
});
});
const {
env: {
PORT = 5000,
} = {},
} = process;
app.listen(PORT);