-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
58 lines (52 loc) · 1.36 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const express = require('express');
const multer = require('multer');
const cors = require('cors');
const { nanoid } = require('nanoid');
const { unlink } = require('fs/promises');
const fs = require('fs');
const https = require('https');
require('dotenv').config();
const app = express();
app.use(
express.static('public', {
setHeaders: (res, path, status) => {
res.set('Content-Disposition', 'attachment');
},
})
);
app.use(cors());
app.disable('x-powered-by');
const upload = multer({
storage: multer.diskStorage({
destination: 'public/img/',
filename: (req, file, cb) => cb(null, `${nanoid(16)}.jpg`),
}),
});
app.post('/upload', upload.array('images'), (req, res) => {
res
.status(200)
.json(req.files.map((x) => ({ [x.originalname]: `/img/${x.filename}` })));
});
app.delete('/img/:path', async (req, res) => {
try {
await unlink(`public/img/${req.params.path}`);
res.status(204).json(`${req.params.path} deleted`);
} catch (err) {
res.status(500).json('something went wrong');
}
});
if (process.env.HTTPS) {
const server = https.createServer(
{
cert: fs.readFileSync(process.env.CERT_PATH),
key: fs.readFileSync(process.env.KEY_PATH),
...(process.env.CA_CERT_PATH && {
ca: fs.readFileSync(process.env.CA_CERT_PATH),
}),
},
app
);
server.listen(process.env.PORT || 3001);
} else {
app.listen(process.env.PORT || 3001);
}