Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
97 lines (84 loc) · 2.59 KB
/
Copy pathserver.js
File metadata and controls
97 lines (84 loc) · 2.59 KB
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
// Get dependencies// Get de
const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
const app = express();
var multer = require('multer');
// set the directory for the uploads to the uploaded to
var DIR = './src/assets/images/uploads';
// read and parse application/json
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
var storage = multer.diskStorage({
destination: DIR,
filename: function (req, file, cb) {
cb(null, Date.now() + '_' + (Math.random() * (Number.MIN_SAFE_INTEGER - Number.MAX_SAFE_INTEGER) + Number.MAX_SAFE_INTEGER).toString() + path.extname(file.originalname))
}
});
var upload = multer({
storage: storage,
fileFilter: function(req, file, cb){
checkFileType(file, cb);
}
}).any();
function checkFileType(file, cb){
// Allowed extentions
let ext = /jpeg|jpg/;
// Check file extention
let filename = ext.test(path.extname(file.originalname).toLowerCase());
// Check file mimetype
let mimetype = ext.test(file.mimetype);
if(filename && mimetype){
return cb(null, true);
}else{
cb('Типът на файла не се поддържа');
}
}
// Point static path to dist
app.use(express.static(path.join(__dirname, 'dist')));
// Public Folder
app.use(express.static(DIR));
app.post('/upload', (req,res)=>{
upload(req, res, (err) => {
if(err){
res.send({success:false, msg: err});
} else {
if(req.files === undefined){
res.send({success: false, msg: 'Няма избран файл.'});
}else{
res.send({
success: true,
msg: 'Файлът е успешно качен',
fileName: req.files[0].filename
});
}
}
});
});
//create a cors middleware
app.use(function(req, res, next) {
//set headers to allow cross origin request.
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
// Catch all other routes and return the index file
app.get('*', (req, res) => {
console.log(__dirname);
res.sendFile(path.join(__dirname, 'dist/index.html'));
});
/**
* Get port from environment and store in Express.
*/
const port = process.env.PORT || '3000';
app.set('port', port);
/**
* Create HTTP server.
*/
const server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port, () => console.log(`Photogram running on localhost:${port}`));