-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
57 lines (48 loc) · 1.65 KB
/
app.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
//Installing express dotenv and bodyparser package
const express = require('express');
const dotenv = require('dotenv');
const bodyParser = require('body-parser');
const path = require('path');
const app = express();
dotenv.config();
//Routes
const allRoutes = require('./utilities/connections/allRoutes');
// Set static folder
app.use(express.static(path.join(__dirname, './assets')));
app.use(bodyParser.urlencoded({ extended: true, limit: '7mb' }));
app.use(bodyParser.json({limit: '7mb'}));
//CORS
//Middle ware function
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', '*');
res.setHeader('Access-Control-Allow-Headers', '*');
next();//Call next middleware
});
//Inatializing Routes
app.use(allRoutes.user);
app.use(allRoutes.worker);
app.use(allRoutes.assignwork);
app.use(allRoutes.category);
app.use(allRoutes.dashboard);
//Database connection
const sequelize = require('./utilities/connections/mysqlConnection');
const { all } = require('./routes/userRoute');
//404 (Route NOt Found Middleware)
app.use((req, res, next) => {
const statusCode = 404;
const endPoints = req.url;
const method = req.method;
res.status(statusCode).json({
message: `${method} method is not allowed for this ${endPoints} endpoint or ${endPoints} not found`
});
});
//Error Handling or Error Response middleware
app.use((error, req, res, next) => {
const statusCode = error.statusCode || 500;
const message = error.message || error;
//console.log(error)
res.status(statusCode).json({ message });
});
sequelize.sync()
app.listen(process.env.PORT || 3010);