-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
68 lines (54 loc) · 1.93 KB
/
app.js
File metadata and controls
68 lines (54 loc) · 1.93 KB
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
// * Import required modules
const express = require('express');
const dotenv = require('dotenv');
const db = require('./src/models');
const jwt = require('jsonwebtoken');
const path = require('path');
const cors = require('cors');
// * Get config variables
dotenv.config();
// * Authenticate Database connection
db.sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch((err) => {
console.error('Unable to connect to the database:', err);
});
// * Sync Database tables
if (process.env.ALLOW_SYNC === 'true') {
db.sequelize.sync({ alter: true }).then(() => console.log('Done adding/updating the database based on the Models.'));
}
// * Initialize express
var app = express();
const PORT = process.env.PORT || 5000;
app.use(express.json());
app.use(
express.urlencoded({
extended: true,
})
);
// * Cors
app.use(cors());
const authenticateToken = (req, res, next) => {
const authHeader = req.headers['authorization'];
const token = authHeader && authHeader.split(' ')[1];
if (token == null) return res.sendStatus(401);
jwt.verify(token, process.env.TOKEN_SECRET, (err, user) => {
if (err) return res.sendStatus(403);
req.user = user;
next();
});
};
app.use('/public', express.static(path.join(__dirname + '/public/uploads/')));
app.use(`${process.env.API_VERSION}/home`, require('./src/routes/home.routes'));
app.use(`${process.env.API_VERSION}/test`, require('./src/routes/test.routes'));
app.use(`${process.env.API_VERSION}/resto-admin`, authenticateToken, require('./src/routes/resto_admin.routes'));
app.use(`${process.env.API_VERSION}/admin`, authenticateToken, require('./src/routes/admin.routes'));
app.use(`${process.env.API_VERSION}/customer`, authenticateToken, require('./src/routes/customer.routes'));
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}.`);
});
// TODO: Generate Token
// console.log(require('crypto').randomBytes(64).toString('hex'));