-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
65 lines (53 loc) · 1.5 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
//Import Main Modules
const express = require('express');
const bodyParser = require("body-parser");
const cors = require('cors');
const cookieParser = require('cookie-parser')();
const Knex = require('knex');
const { Model } = require('objection');
const app = express();
app.use(cookieParser);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cors({ origin: false }));
//Import Configs (to be moved in .nev)
const { mysqlDbConfig } = require('./config');
//Settings
const PORT = process.env.PORT ? process.env.PORT : 8383;
// Initialize knex.
const db = Knex({
client: 'mysql',
connection: {
host: mysqlDbConfig.HOST,
database: mysqlDbConfig.DB,
user: mysqlDbConfig.USER,
password: mysqlDbConfig.PASSWORD,
options: {
port: mysqlDbConfig.PORT
}
},
debug: true,
pool: {
min: 2,
max: 10,
afterCreate: (conn, done) => {
console.log('Connection made!');
done(false, conn)
}
}
});
// Give the knex instance to objection.
Model.knex(db);
app.get("/", async (req, res) => {
try {
let data = await db.select("*").from('customers');
res.json({ data: data });
} catch (error) {
res.json({ message: "Api error", error: error });
}
});
app.use('/customer/', require('./Routes/CustomerRouter')({ db: db }));
app.get("*", async (req, res) => {
res.json({ message: "Nothing here. Try an existing route instead?" });
});
app.listen(Number(PORT), () => console.log(`Server is up an runing on port: ${PORT}`));