forked from indraarianggi/nodejs-sequelize-mysql-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
59 lines (47 loc) · 1.31 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
const express = require("express");
const bodyParser = require("body-parser");
const cors = require("cors");
const config = require("./app/config/config.js");
const app = express();
const 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 }));
// database
const db = require("./app/models");
const Role = db.role;
db.sequelize.sync().then(() => {
initial(); // Just use it in development, at the first time execution!. Delete it in production
});
// simple route
app.get("/", (req, res) => {
res.json({ message: "Hi there, welcome to this tutorial." });
});
// api routes
require("./app/routes/book.routes")(app);
require("./app/routes/auth.routes")(app);
require("./app/routes/user.routes")(app);
// set port, listen for requests
const PORT = config.PORT;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
// Just use it in development, at the first time execution!. Delete it in production
function initial() {
Role.create({
id: 1,
name: "user"
});
Role.create({
id: 2,
name: "moderator"
});
Role.create({
id: 3,
name: "admin"
});
}