-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
55 lines (42 loc) · 1.17 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
const express = require("express");
const cors = require("cors");
const requestIp = require("request-ip");
const UAParser = require("ua-parser-js");
require("dotenv").config();
const PORT_NO = process.env.PORT || 3030;
const app = express();
app.use(express.json());
app.use(cors());
app.use(requestIp.mw());
// Import your routes
const publicRoutes = require("./routes/public");
// Middleware to attach user object to request
app.use((req, res, next) => {
const useragent = req.headers["user-agent"];
let parser = new UAParser(useragent);
let parserResults = parser.getResult();
// Simulate user object creation
const user = {
ip: req.clientIp,
deviceInfo: parserResults,
activity: [],
};
// Attach the user object to the request object
req.user = user;
next();
});
// Use your routes
app.use(publicRoutes);
// Error handling middleware
app.use((error, req, res, next) => {
if (!res.headersSent) {
res.status(error.httpStatusCode || 500).json({
status: "error",
message: error.message || "An unexpected error occurred.",
});
}
});
// Start the server
app.listen(PORT_NO, () => {
console.log(`Server Running On Port ${PORT_NO}`);
});