-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
40 lines (35 loc) · 1.09 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
var Express = require('express');
var multer = require('multer');
var randomstring = require("randomstring");
var basicAuth = require('basic-auth-connect');
var app = Express();
app.use(require('cors')());
var auth = basicAuth('admin', process.env.PASSWORD || 'password')
var Storage = multer.diskStorage({
destination: function(req, file, callback) {
callback(null, "./files");
},
filename: function(req, file, callback) {
callback(null, randomstring.generate(7) + "_" + file.originalname);
}
});
var upload = multer({
storage: Storage,
limits: {
fileSize: 5 * 1024 * 1024
}
}).single("file");
app.get("/", auth, function(req, res) {
res.sendFile(__dirname + "/index.html");
});
app.post("/", auth, function(req, res) {
upload(req, res, function(err, val) {
if (err) res.json({ error: 'Error uploading file. ' + err.toString() })
else res.json({
filename: process.env.PUBLIC_FRONTEND_URL + '/' + (req.file && req.file.filename)
})
});
});
app.listen(8080, function(a) {
console.log("Listening to port 8080");
});