-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
59 lines (44 loc) · 1.47 KB
/
app.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
const express = require('express');
const multer = require('multer');
const path = require('path');
const axios = require('axios');
const fs = require('fs');
const app = express();
const port = 80;
const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, 'uploads/');
},
filename: (req, file, cb) => {
cb(null, `${file.fieldname}-${Date.now()}${path.extname(file.originalname)}`);
}
});
const upload = multer({ storage });
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.set('view engine', 'ejs');
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.post('/upload', upload.single('audio'), async (req, res) => {
const fileUrl = 'http://localhost:5000/upload';
try {
if (!req.file) {
throw new Error('No file uploaded');
}
const uploadedFilePath = req.file.path;
const response = await axios.post(fileUrl, { fileUrl: req.file.path });
if (response.data.error) {
throw new Error(response.data.error);
}
const result = response.data.result.text;
fs.unlinkSync(uploadedFilePath);
res.render("result", { result: result });
} catch (error) {
console.error('Error processing file:', error.message);
res.status(500).send('Internal Server Error');
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});