-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
54 lines (43 loc) · 1.37 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
const express = require('express');
const bodyParser = require('body-parser');
require('dotenv').config();
const http = require('http');
const cors = require('cors');
const sequelize = require('./src/utils/database');
const rotasFuncionarios = require('./src/routes/funcionarios');
const rotasCargos = require('./src/routes/cargos');
const defaultLogger = require('./src/utils/logger');
const Cargo = require('./src/models/cargo');
const Funcionario = require('./src/models/funcionario');
const app = express();
app.use(cors());
app.options('*', cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(defaultLogger);
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
app.use('/cargos', rotasCargos);
app.use('/employee', rotasFuncionarios);
app.use((req, res, next) => res.status(404).json({ error: "This route does not exist." }));
Cargo.hasMany(Funcionario, {
foreignKey: {
name: 'cargoId',
allowNull: false
}
});
Funcionario.belongsTo(Cargo);
const port = process.env.PORT || 8080;
const server = http.createServer(app);
sequelize.sync()
.then(() => {
console.log("Listening on port:", port);
server.listen(port);
})
.catch(err => {
console.log(err)
return -1;
});