-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
74 lines (59 loc) · 2.16 KB
/
Copy pathserver.js
File metadata and controls
74 lines (59 loc) · 2.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
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
const express = require('express');
const multer = require('multer');
const { spawn } = require('child_process');
const path = require('path');
const cors = require('cors');
const fs = require('fs');
const app = express();
const port = 5000;
// Enable CORS
app.use(cors());
// Configure Multer for file uploads
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/');
},
filename: function (req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname));
}
});
const upload = multer({ storage: storage });
// Ensure the uploads directory exists
if (!fs.existsSync('uploads')) {
fs.mkdirSync('uploads');
}
// Add a simple route for the root URL
app.get('/', (req, res) => {
res.send('Image Comparison Server is running');
});
// Endpoint to handle image uploads
app.post('/upload', upload.single('file'), (req, res) => {
const filePath = req.file.path;
const fileUrl = `${req.protocol}://${req.get('host')}/${filePath}`;
res.json({ imageUrl: fileUrl });
});
// Endpoint to handle image comparison
app.post('/compare', upload.single('file'), (req, res) => {
const serverImageUrl = req.body.serverImageUrl;
const localImagePath = req.file.path;
const localImageDirectory = "C:\\Users\\nhl08\\OneDrive\\Máy tính\\imagesipfs\\image-comparison-server\\image";
const pythonProcess = spawn('python3', ['compare_images.py', localImagePath, serverImageUrl, localImageDirectory]);
pythonProcess.stdout.on('data', (data) => {
const similarity = parseFloat(data.toString());
res.json({ similarity });
});
pythonProcess.stderr.on('data', (data) => {
console.error(`stderr: ${data}`);
res.status(500).send(data.toString());
});
pythonProcess.on('close', (code) => {
// Clean up the uploaded file
fs.unlinkSync(localImagePath);
console.log(`child process exited with code ${code}`);
});
});
// Serve static files from the uploads directory
app.use('/uploads', express.static(path.join(__dirname, 'uploads')));
app.listen(port, () => {
console.log(`Server running on http://116.109.144.226:${port}`);
});