-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
108 lines (91 loc) · 3.07 KB
/
index.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import 'express-async-errors';
import { join } from 'path';
import { createServer } from 'http';
import express, { json, urlencoded } from 'express';
import helmet from 'helmet';
import connectDB from './startup.js';
import taxRoutes from './routes/taxrate.route.js';
import productRoutes from './routes/product.route.js';
import customerRoutes from './routes/customer.route.js';
import orderRoutes from './routes/order.route.js';
import userRoutes from './routes/user.route.js';
import authRoutes from './routes/auth.route.js';
import infoRoutes from './routes/info.route.js';
import companyRoutes from './routes/company.route.js';
import errorHandler from './middlewares/error-handler.js';
import { logger } from './helpers/logger.js';
import path from 'path';
const __dirname = path.resolve();
const app = express();
process.on("uncaughtException", (error) => {
logger.log(error.message, error);
});
process.on("unhandledRejection", (error) => {
throw error;
});
connectDB();
const normalizePort = val => {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
};
const onError = error => {
if (error.syscall !== "listen") {
throw error;
}
const bind = typeof addr === "string" ? "pipe " + addr : "port " + port;
switch (error.code) {
case "EACCES":
console.error(bind + " requires elevated privileges");
process.exit(1);
break;
case "EADDRINUSE":
console.error(bind + " is already in use");
process.exit(1);
break;
default:
throw error;
}
};
const onListening = () => {
const addr = server.address();
const bind = typeof addr === "string" ? "pipe " + addr : "port " + port;
console.log("Listening on " + bind);
};
const port = normalizePort(process.env.PORT || "3000");
app.set("port", port);
// Express Config Settings
app.use(json());
app.use(urlencoded({extended: true}));
app.use(helmet({contentSecurityPolicy: false}));
app.use((req,res,next) => {
res.setHeader('Access-Control-Allow-Origin','*');
res.setHeader('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept, Authorization, User, x-auth-token');
res.setHeader('Access-Control-Allow-Methods','GET, POST, PUT, DELETE, OPTIONS');
next();
});
app.use('/', express.static(join(__dirname, "hisaab")));
// Routes
app.use('/api/taxrates', taxRoutes);
app.use('/api/products', productRoutes);
app.use('/api/customers', customerRoutes);
app.use('/api/orders', orderRoutes);
app.use('/api/users', userRoutes);
app.use('/api/auth', authRoutes);
app.use('/api/company', companyRoutes);
app.use('/api/info', infoRoutes);
app.use((req, res) => {
res.sendFile(join(__dirname, 'hisaab', 'index.html'));
});
app.use(errorHandler);
const server = createServer(app);
server.on("error", onError);
server.on("listening", onListening);
server.listen(port);