-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
43 lines (33 loc) · 1.16 KB
/
index.js
File metadata and controls
43 lines (33 loc) · 1.16 KB
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
const express = require('express')
const fs = require('fs')
const path = require('path')
const app = express()
const PORT = 4001
app.get('/', (req, res) => {
res.send('Welcome to nodejs-filesystem')
})
// endpoint to create a text file with the current timestamp
app.post('/createFile', (req, res) => {
const timestamp = Date.now() //current timestamp in milliseconds since the Unix Epoch
// name of the file
const fileName = new Date().toISOString().replace(/[-:]/g, '_') + '.txt'
// path of the file
const filePath = path.join(__dirname, 'files', fileName)
// write content of the file
fs.writeFileSync(filePath, timestamp.toString())
res.send('File created successfully')
})
// endpoint to retrieve all text files in the directory named 'files'
app.get('/all', (req, res) => {
const directoryPath = path.join(__dirname, 'files')
// read all the files in the directory named 'files'
fs.readdir(directoryPath, (err, files) => {
if (err) {
res.status(500).send('Error reading the files!')
}
res.send(files)
})
})
app.listen(PORT, () => {
console.log(`Server is starting on port:${PORT}`)
})