-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
105 lines (89 loc) · 3.17 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const fs = require('fs');
const path = require('path');
//const https = require('https');
const express = require('express');
const bodyParser = require('body-parser');
const multer = require('multer');
const { v4: uuidv4 } = require('uuid');
const helmet = require('helmet');
const morgan = require('morgan');
const app = express();
const feedRoutes = require('./routes/feed_router');
const authRoutes = require('./routes/auth_router');
const userRoutes = require('./routes/user_router');
const accessLogStream = fs.createWriteStream(
path.join(__dirname, 'requests.log'),
{ flags: 'a' } // a = append (new data will be appended to the log file)
);
app.use(helmet());
app.use(morgan('combined', { stream: accessLogStream }));
//const privateKey = fs.readFileSync('server.key');
//const certificate = fs.readFileSync('server.cert');
const profilePictures = multer({ dest: 'profilePictures/' });
const productPictures = multer({ dest: 'productPictures/' });
const fileStorage = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, 'images');
},
//destination: "./images/",
filename: (req, file, callback) => {
//callback(null, new Date().toISOString() + "-" + file.originalname); //doesn't work in windows
callback(null, uuidv4());
},
});
const fileFilter = (req, file, callback) => {
console.log('fileFilter', file);
if (
file.mimetype === 'image/png' ||
file.mimetype === 'image/jpg' ||
file.mimetype === 'image/jpeg'
) {
callback(null, true); //valid file
} else {
callback(null, false); //invalid file
}
};
app.use(bodyParser.json()); // parse incoming JSON data
app.use(bodyParser.urlencoded({ extended: true }));
app.use(
multer({ storage: fileStorage, fileFilter: fileFilter }).single('image')
//extract a single file in a field named 'image' in the request
); //every incoming request will be parsed for files
app.use(
'/profilePictures',
express.static(path.join(__dirname, 'profilePictures'))
);
app.use(
'/productPictures',
express.static(path.join(__dirname, 'productPictures'))
);
app.use((req, res, next) => {
//middleware to solve CORS error
res.setHeader('Access-Control-Allow-Origin', '*'); //allow origins to access my data
res.setHeader(
'Access-Control-Allow-Methods',
'GET, POST, PUT, PATCH, DELETE'
); //allow origins to use my HTTP methods
res.setHeader(
'Access-Control-Allow-Headers',
'Content-type, Accept, Authorization'
); //allow origins to use these two headers
next(); //the request can now continue
});
app.use('/feed', feedRoutes);
app.use('/user', userRoutes);
app.use('/auth', authRoutes);
app.use((error, req, res, next) => {
// executed whenever an error is thrown with throw() or forwarded with next()
console.log('Erro capturado:', error);
const status = error.statusCode || 500; // if error.statusCode is undefined, then status = 500
const message = error.message;
const data = error.data;
//if (!message.includes('invalid signature')){
res.status(status).json({ message: message, data: data });
//}
});
//https
// .createServer({ key: privateKey, cert: certificate }, app)
// .listen(process.env.PORT || 8080);
app.listen(process.env.PORT || 8080);