forked from lucivandosousa/api-express-array
-
Notifications
You must be signed in to change notification settings - Fork 5
/
app.js
88 lines (75 loc) · 2.51 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
// Importando o módulo express, que é um framework para construção de aplicações web em Node.js.
const express = require("express");
// Importando o módulo swagger-jsdoc que lê o código e gera a especificação.
const swaggerJSDoc = require("swagger-jsdoc");
// Importando o módulo swagger-ui-express.
const swaggerUi = require("swagger-ui-express");
// Importando o módulo cors
const cors = require("cors");
//Pegar diretorio dos arquivos do sistema OS
const path = require("path");
// Validar token do JWT
const { authenticateToken } = require("./helps/jwt");
// Criando uma instância da aplicação Express.
const app = express();
// Middleware para permitir que a aplicação interprete JSON nas requisições.
app.use(express.json());
// Add Cors
app.use(cors());
// Rotas
const auth = require("./routes/auth");
const produtosRouter = require("./routes/produtos");
const usuariosRouter = require("./routes/usuarios");
const pedidosRouter = require("./routes/pedidos");
//Adicionando os meu arquivos publicos
app.use(express.static(path.join(__dirname, "/public")));
// Definindo a porta em que a aplicação vai rodar.
const port = process.env.PORT || 3000;
// Configuração do Swagger
const swaggerDefinition = {
openapi: "3.0.0",
info: {
title: "Exemplo de API CRUD com Express",
version: "1.0.0",
description:
"Documentação da API CRUD com Express e persistência em um Array de dados",
},
servers: [
//TODO:: Mudar par o seu projeto
{
url: `https://api-express-array.vercel.app`,
description: "Servidor de produção",
},
{
url: `http://localhost:${port}`,
description: "Servidor local",
},
],
};
const options = {
swaggerDefinition,
apis: ["app.js"],
};
const swaggerSpec = swaggerJSDoc(options);
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerSpec));
app.get("/versao-valid", (req, res) => {
res.sendFile(__dirname + "/public/index.html");
});
// Endpoint inicial para verificar se a API está funcionando.
/**
* @swagger
* /:
* get:
* summary: Endpoint inicial para verificar se a API está funcionando
* responses:
* 201:
* description: OK
*/
app.use("/", auth);
// Middleware para proteger os endpoints
// Autenticado
app.use("/produtos", authenticateToken, produtosRouter);
app.use("/usuarios", authenticateToken, usuariosRouter);
app.use("/pedidos", authenticateToken, pedidosRouter);
// Iniciando o servidor na porta definida.
app.listen(port, () => console.log(`Server listening on port ${port}`));