-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.ts
68 lines (52 loc) · 1.65 KB
/
server.ts
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
import path from 'path';
import dotenv from 'dotenv';
let envPath = '';
switch (process.env.ENVIRONMENT) {
case 'db':
case 'prod':
case 'dev': envPath = './.env'; break;
case 'test': envPath = './.env.test'; break;
default: console.log('ENV ERROR : Please check if the ENVIRONNEMENT variable is configured !'); process.exit(1);
}
dotenv.config({ path: path.resolve(__dirname, envPath) });
import express from "express";
import bodyParser from "body-parser";
import cors from "cors";
import authRoute from './app/routes/auth.routes';
import userRoute from './app/routes/user.routes';
import bookRoute from './app/routes/book.routes';
import { sequelize } from "./sequelize";
const app = express();
var corsOptions = {
origin: "http://localhost:8081"
};
app.use(cors(corsOptions));
// parse requests of content-type - application/json
app.use(bodyParser.json());
// parse requests of content-type - application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }));
if (process.env.ENVIRONMENT === "db") {
const seeder = require("./app/seeders");
sequelize.sync({ force: true }).then(async () => {
console.log('[DB GENERATE] Drop, seed and Resync Db');
await seeder.seed();
console.log('[DB GENERATE] Done')
process.exit(0);
});
} else {
sequelize.sync();
}
// simple route
app.get("/", (req: any, res: any) => {
res.json({ message: "Welcome to CESI ton livre API." });
});
// Advanced routes
authRoute(app);
userRoute(app);
bookRoute(app);
// set port, listen for requests
const PORT = process.env.PORT || 8080;
app.listen(PORT,() => {
console.log(`Server is running on port ${PORT}.`);
});
export default app;