-
Notifications
You must be signed in to change notification settings - Fork 24
/
App.js
81 lines (66 loc) · 2.73 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/***************************************************************
node.js express app file form server upload w/ Multer demo
App created by: Jesse Lewis
Multer Config used based on tutorial by Ashish Mehra via Youtube
@ https://www.youtube.com/watch?v=sMnqnvW81to&lc=z23htp54jwmhwni0nacdp43axbwhgu3y3fg0jwzwhatw03c010c
******************************************************************************************************/
// RUN PACKAGES
const express = require('express');
const multer = require('multer');
const bodyParser = require('body-parser');
// SETUP APP
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());
app.use('/', express.static(__dirname + '/public'));
//MULTER CONFIG: to get file photos to temp server storage
const multerConfig = {
//specify diskStorage (another option is memory)
storage: multer.diskStorage({
//specify destination
destination: function(req, file, next){
next(null, './public/photo-storage');
},
//specify the filename to be unique
filename: function(req, file, next){
console.log(file);
//get the file mimetype ie 'image/jpeg' split and prefer the second value ie'jpeg'
const ext = file.mimetype.split('/')[1];
//set the file fieldname to a unique name containing the original name, current datetime and the extension.
next(null, file.fieldname + '-' + Date.now() + '.'+ext);
}
}),
// filter out and prevent non-image files.
fileFilter: function(req, file, next){
if(!file){
next();
}
// only permit image mimetypes
const image = file.mimetype.startsWith('image/');
if(image){
console.log('photo uploaded');
next(null, true);
}else{
console.log("file not supported")
//TODO: A better message response to user on failure.
return next();
}
}
};
/* ROUTES
**********/
app.get('/', function(req, res){
res.render('index.html');
});
app.post('/upload', multer(multerConfig).single('photo'),function(req, res){
//Here is where I could add functions to then get the url of the new photo
//And relocate that to a cloud storage solution with a callback containing its new url
//then ideally loading that into your database solution. Use case - user uploading an avatar...
res.send('Complete! Check out your public/photo-storage folder. Please note that files not encoded with an image mimetype are rejected. <a href="index.html">try again</a>');
}
);
// RUN SERVER
app.listen(port,function(){
console.log(`Server listening on port ${port}`);
});