-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
75 lines (58 loc) · 1.78 KB
/
server.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
const db = require("./Models");
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cors = require("cors");
var bodyParser = require('body-parser');
const allowedOrigins = ['https://localhost:4200','http://localhost:4200', 'http://localhost:10000','https://localhost:10000',
'https://mapapp-edlw.onrender.com', 'https://mapappbackend.onrender.com/api', "https://api.ipify.org/?format=json" ,
'https://rues-sans-peur.org'];
var corsOptions = {
origin: function (origin, callback) {
// Check if the request origin is in the allowedOrigins array
if (!origin || allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
}
};
db.sequelize.sync({ force:false })
.then(() => {
console.log('Tag table created!');
})
.catch(err => {
console.error('Error creating Tag table: ', err);
});
const tagRoute = require('./routes/tag.routes.')
const app = express();
app.use(cors(corsOptions));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(express.static(path.join(__dirname, 'MapApp/app/index.html')));
// API root
app.use('/api', tagRoute)
// PORT
const port = process.env.PORT || 10000;
app.listen(port, () => {
console.log('Listening on port ' + port)
})
// 404 Handler
app.use((req, res, next) => {
next(createError(404));
});
// Base Route
app.get('/', (req, res) => {
res.send('invaild endpoint');
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'MapApp/src/app/index.html'));
});
// error handler
app.use(function (err, req, res, next) {
console.error(err.message);
if (!err.statusCode) err.statusCode = 500;
res.status(err.statusCode).send(err.message);
});