-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
70 lines (61 loc) · 2.1 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
58
59
60
61
62
63
64
65
66
67
68
69
70
const express = require("express");
const fs = require("fs");
const path = require("path");
const bodyParser = require("body-parser");
const morgan = require("morgan");
const staticVariables = require("./const/static-variables");
const accessManagerRouter = require("./services/AccessManager/index");
const baseInfoRouter = require("./services/baseInfo/index");
const personRouter = require("./services/person/index");
const systemRouter = require("./services/system/index");
// const personRouter = require("./services/person/index");
const HttpError = require("./util/http-error");
const app = express();
let init = require("./initializer");
init.runInitializer();
console.log("Envirement: " + process.env.NODE_ENV);
app.use(morgan("tiny"));
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use("/uploads/images", express.static(path.join("uploads", "images")));
//گرفتن زبان برای نمایش خطاها
app.use((req, res, next) => {
req.ip = req.headers["x-forwarded-for"] || req.connection.remoteAddress;
req.language = req.query.language;
if (!req.language)
req.language = req.headers.language || staticVariables.DEFAULT_LANGUGE;
next();
});
// 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"
// );
// res.setHeader("Access-Control-Allow-Methods", "GET, POST, PATCH, DELETE");
// next();
// });
app.use("/api/", accessManagerRouter);
app.use("/api/", baseInfoRouter);
app.use("/api/", personRouter);
app.use("/api/", systemRouter);
app.use((req, res, next) => {
const error = new HttpError("could not found this route", 404);
throw error;
});
app.use((error, req, res, next) => {
if (req.file) {
fs.unlink(req.file.path, (err) => {
console.log(err);
});
}
if (res.headerSent) {
return next(error);
}
res.status(error.statusCode || 500);
res.json({
code: error.code || -1,
message: error.message || "an unknown error occurred!",
});
});
module.exports = app;