-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileServer.js
More file actions
59 lines (29 loc) · 749 Bytes
/
fileServer.js
File metadata and controls
59 lines (29 loc) · 749 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
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
const express = require('express');
const fs = require('fs');
const path = require('path');
const app = express();
app.get("/files",function(req,res){
fs.readdir(path.join(__dirname,'./files/'), function(err,files){
if(err){
return res.status(500).json({
error :'failed to retreive files '
})
}
else{
res.json(files);
}
});
});
app.get('/file/:filename', function (req, res) {
const filepath = path.join(__dirname, './files/', req.params.filename);
fs.readFile(filepath, 'utf8', (err, data) => {
if (err) {
return res.status(404).send('File not found');
}
res.send(data);
});
});
app.all('*', (req, res) => {
res.status(404).send('Route not found');
});
module.exports = app;