-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathserver.js
63 lines (55 loc) · 1.78 KB
/
server.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
const express = require('express')
const multer = require('multer')
const path = require('path')
const fs = require('fs')
const app = express()
const PORT = 3000
const staticPath = path.join(__dirname, '/public')
app.use(express.static(staticPath))
app.use('/style', express.static(path.join(staticPath, '/style.css')))
app.use('/script', express.static(path.join(staticPath, '/script.js')))
const upload = multer({
storage: multer.diskStorage({
destination: function (req, file, callback) {
if (!fs.existsSync('uploads')) {
fs.mkdirSync('uploads', { recursive: true })
}
callback(null, 'uploads')
},
filename: function (req, file, callback) {
callback(null, '_' + Date.now() + file.originalname)
},
}),
}).single('file_data')
app.get('/', (req, res) => {
res.status(200).send('Welcome to the File Uploaded Server ❤️ -> Go Ahead and Show Your Skills on our Codebase ')
})
const cheerio = require('cheerio');
app.get('/listfile', (req, res) => {
if (fs.existsSync("./uploads")) {
const dirpath = path.join(__dirname, "uploads");
const $ = cheerio.load(fs.readFileSync(__dirname + '/public/files.html'));
fs.readdir(dirpath, (err, files) => {
if (files.length > 0) {
for (i in files) {
$('#filesList').append("<tr><td>" + files[i] + "</td></tr>")
}
// res.send(files);
res.send($.html())
}
else {
res.send("There are no files in the uploads folder");
}
})
}
else {
res.send("There is no uploads folder");
}
})
app.post('/upload', upload, (req, res) => {
console.log('File ' + req.file.originalname + ' Uploaded')
res.status(200).send({ status: 'File uploaded' })
})
app.listen(PORT, () => {
console.log(`Running on PORT http://localhost:${PORT}`)
})