-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
48 lines (38 loc) · 1.35 KB
/
index.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
const express = require('express');
const bodyParser = require('body-parser');
const dotenv = require('dotenv');
const fs = require('fs');
const cors = require('cors');
dotenv.config();
const swaggerDoc = require('./swaggerDoc.js');
const app = express();
const PORT = process.env.PORT || 31901;
//app.use(bodyParser.json());
//app.use(bodyParser.urlencoded({ extended: true }));
app.use('/*', (req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', '*');
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}))
app.use(cors({origin: '*'}));
swaggerDoc(app);
const Folder = './Routes/';
fs.readdir(Folder, (err, files) => {
files.forEach(file => {
var Path = require("path").join(__dirname, Folder + file +"/");
require("fs").readdirSync(Path).forEach(function(file2) {
app.use('/api/' + file.toLowerCase(), require(Folder + file + "/" + file2));
});
});
});
const db = require ('./models/db.js');
db.sequelize.sync()
.then(() => console.log("Uspjesno povezivanje sa bazom!"))
.catch((err) => console.log("Povezivanje sa bazom nije uspjelo!", err));
app.listen(PORT, () => {
console.log(`Rest-api service started on ${PORT} port!`);
});
module.exports = app;