-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
77 lines (64 loc) · 2.32 KB
/
server.js
File metadata and controls
77 lines (64 loc) · 2.32 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
const express = require('express');
const multer = require('multer');
const path = require('path');
const fs = require('fs'); // Import the file system module
const app = express();
const PORT = 8080; // Use port 8080
// Middleware to serve static files from the current directory
app.use(express.static(path.join(__dirname))); // Serve static files
// Set up storage for uploaded files
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/'); // Specify the upload directory
},
filename: (req, file, cb) => {
cb(null, file.originalname); // Preserve the original filename
}
});
const upload = multer({ storage: storage });
// Middleware to parse JSON bodies
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Sample clipboard history data
let clipboardHistory = [];
let fileHistory = [];
// Endpoint for uploading files
app.post('/upload', upload.single('file'), (req, res) => {
fileHistory.unshift(req.file.originalname); // Add to file history at the top
res.send({ message: 'File uploaded successfully!', filename: req.file.originalname });
});
// Endpoint to save clipboard data
app.post('/clipboard', (req, res) => {
const { text } = req.body;
clipboardHistory.unshift(text); // Add to history at the top
res.sendStatus(200);
});
// Endpoint to retrieve clipboard history
app.get('/clipboard/history', (req, res) => {
res.json({ history: clipboardHistory });
});
// Endpoint to retrieve file history
app.get('/files', (req, res) => {
res.json({ files: fileHistory });
});
// Endpoint to remove uploaded files
app.delete('/files/:filename', (req, res) => {
const fileName = req.params.filename;
const filePath = path.join(__dirname, 'uploads', fileName);
fs.unlink(filePath, (err) => {
if (err) {
return res.status(500).send('Failed to delete file');
}
fileHistory = fileHistory.filter(file => file !== fileName); // Remove file from history
res.sendStatus(200); // OK response
});
});
// Endpoint to clear clipboard history
app.delete('/clipboard/history', (req, res) => {
clipboardHistory = []; // Clear the history
res.sendStatus(200); // OK response
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});