-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
executable file
·67 lines (51 loc) · 2.31 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
64
65
66
67
const express = require('express');
const fileUpload = require('express-fileupload');
const cors = require('cors');
const fs = require('fs');
// Init express
const server = express();
server.use(fileUpload());
server.use(cors());
// Listens for post requests and handles the uploading of a video file
server.post('/upload', (req, res) => {
if(req.files === null){
return res.status(400).json({ msg: 'Upload failed!' });
}
// Retrieve the file object from the request
const newFile = req.files.file;
const parsedFileName = newFile.name.replaceAll(' ', '');
// Store the new file in the uploads folder
newFile.mv(`${__dirname}/client/public/uploads/${parsedFileName}`, err => {
// If there was an error saving the file, log it and send status 500 (internal server error)
if(err){
console.error(err);
return res.status(500).send(err);
}
// Retrieve the currently stored list of videos, so we can append the new metadata to it and save
let currentData = JSON.parse(fs.readFileSync('data/videoList.json'));
let newVideo = {
id: currentData.videoList.length,
path: `/uploads/${parsedFileName}`,
name: `${newFile.name}`,
title: `${req.body.videoTitle}`
}
currentData.videoList.push(newVideo);
fs.writeFile('data/videoList.json', JSON.stringify(currentData, null, 2), () => {
console.log("updated json file");
});
// Return info of the file's name and location now that it's uploaded
res.json({ fileName: newFile.name, filePath: `/uploads/${parsedFileName}` });
});
});
// The route 'watch' serves metadata about a specific video (with id), including name and url
server.get('/watch/:id', (req, res) => {
const id = parseInt(req.params.id);
let currentData = JSON.parse(fs.readFileSync('data/videoList.json'));
res.json(currentData.videoList[id]);
});
// The route videoList returns a json object of the list of all video metadata we store
server.get('/videoList', (req, res) => {
res.json(JSON.parse(fs.readFileSync('data/videoList.json')));
});
// Start the server...
server.listen(3001, () => console.log('Video Data Management Server Started...'));