-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
98 lines (69 loc) · 2.01 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
const express = require('express');
const mulder = require('multer');
const path = require('path');
const ejs = require('ejs');
const app = express();
var base_URL = "https://imagix.glitch.me/image";
var conteo;
const storage = mulder.diskStorage({
destination: './public/image',
filename: function (req, file, cb) {
var random = Math.floor(Math.random() * 100) + 1;
cb(null, 'imagix-' + Date.now() + '-'+ random +
path.extname(file.originalname));
}
});
const upload = mulder({
storage: storage,
limits: { fileSize: 800000},
fileFilter: function(req, file, cb){
checkFileType(file, cb);
}
}).single('image');
function checkFileType(file, cb) {
const filetypes = /jpeg|jpg|png|gif/;
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname) {
return cb(null, true);
} else {
cb('Solo imagenes');
};
};
app.set('view engine', 'ejs');
app.use(express.static('./public'));
app.get('/', (req, res) => {
conteo = 0;
res.render('index');
});
app.get('/about', (req, res) =>{
res.render('about');
});
app.get('/test', (req, res) => {
res.render('example');
});
app.post('/upload', (req, res) => {
conteo++;
if (conteo == 2) {
res.redirect('/');
conteo = 0;
} else {
upload(req, res, (err) => {
if (err) {
res.render('index', {
msg: 'IMAGEN MUY GRANDE'
});
} else {
res.render('modal', {
msg: 'Imagen Subida!',
file: `image/${req.file.filename}`,
embed: `<img src="${base_URL}/${req.file.filename}" />`,
url: `${base_URL}/${req.file.filename}`
});
};
});
};
});
const port = 3000;
app.listen(port, () =>
console.log('Server corriendo en el puerto: ' + port));