-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
59 lines (50 loc) · 1.38 KB
/
index.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
// Modules
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const imgur = require('imgur');
const multer = require('multer');
const fs = require('fs');
const path = require('path');
// Express instance
const app = express();
const storage = multer.diskStorage({
destination: './uploads',
filename: (req, file, cb) => {
cb(
null, file.fieldname + '-' + Date.now() + path.extname(file.originalname)
);
},
});
const upload = multer({ storage });
app.use(express.json({ extended: false }));
app.use(express.urlencoded({ extended: false }));
app.use(cors());
app.use(morgan('dev'));
app.use(upload.any());
app.set('view engine', 'ejs');
// GETS
app.get('/', (req, res) => {
res.render('index', {
url: false
});
});
app.post('/uploads', async (req, res) => {
const file = req.files[0];
try {
const url = await imgur.uploadFile(`./uploads/${file.filename}`);
res.render('index', {
url: url.link
});
fs.unlinkSync(`./uploads/${file.filename}`);
} catch (err) {
console.log(err);
res.status(500).json({
status: 500,
err: true,
message: 'Erro interno!'
});
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Backend is running on port ${PORT}`));