-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
45 lines (36 loc) · 1.08 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
const express = require('express');
const uploadFileToGoogleDrive = require('./upload');
const multer = require('multer');
const fs = require('fs');
const cors = require('cors')
const dir = './upload';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
const storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './upload')
},
filename: function (req, file, cb) {
cb(null, file.originalname)
}
})
const upload = multer({ storage: storage })
const app = express();
const router = express.Router();
router.post('/upload', upload.single('image'), (req, res, next) => {
if (!req.file) {
res.status(400).send('No file uploaded.');
return;
}
uploadFileToGoogleDrive(req.file.path, req.file.mimetype, req.file.originalname).then(() => {
console.log('🔥 All files have been uploaded to Google Drive successfully!');
res.status(200).send('File uploaded successfully.');
});
});
app.use(cors())
app.use(express.static(__dirname + '/public'));
app.use('/', router);
app.listen(3000, () => {
console.log('App is listening on port 3000.');
});