-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
79 lines (69 loc) · 2.34 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
const express = require('express');
const app = express();
const morgan = require('morgan');
const bodyParser = require('body-parser');
const cpfRoutes = require('./api/routes/cpf-route');
const ipRoutes = require('./api/routes/ip-route');
const jwtRoutes = require('./api/routes/jwt-route');
const swaggerJsDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');
app.use(morgan('dev')); //enables live reload
app.use(bodyParser.text());
app.use(bodyParser.json()); //extract json data from request body
app.use(bodyParser.urlencoded({extended:true}));
//https://enable-cors.org/server_expressjs.html
app.use((req,res,next)=>{
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
if(req.method === 'OPTIONS'){
//res.header('Access-Control-Allow-Methods','PUT,POST,PATCH,DELETE,GET');
res.header('Access-Control-Allow-Methods','GET');
return res.status(200).json({});
}
if(req.method != ('OPTIONS' && 'GET')){
//res.header('Access-Control-Allow-Methods','PUT,POST,PATCH,DELETE,GET');
return res.status(405).json({
message: 'Method Not Allowed'
});
}
next();
});
// Swagger
const swaggerOptions = {
swaggerDefinition: {
host: 'https://api-utilities.herokuapp.com/',
openapi: '3.0.0',
security: [],
info: {
title: 'Utils',
description: 'Utilidades para desenvolvedores e analistas de teste',
version: 'ALFA',
contact: {
name: 'Lariel',
email: 'lariel.negreiros@gmail.com',
url: 'https://lariel.github.io/',
},
servers: ['http://localhost:3000', 'https://api-utilities.herokuapp.com/']
}
},
apis: ['./api/routes/*.js']
};
const swaggerDocs = swaggerJsDoc(swaggerOptions);
app.use('/cpf', cpfRoutes);
app.use('/ip', ipRoutes);
app.use('/jwt',jwtRoutes);
app.use('', swaggerUi.serve, swaggerUi.setup(swaggerDocs))
app.use((req,res,next)=>{
const error = new Error('Recurso não encontrado');
error.status = 404;
next(error);
});
app.use((error,req,res,next)=>{
res.status(error.status || 500);
res.json({
error: {
message: error.message
}
});
});
module.exports = app;