-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
70 lines (58 loc) · 1.8 KB
/
server.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
import express from "express";
import cors from "cors";
import morgan from "morgan";
import connect from "./Database/conn.js";
import router from "./routes/route.js";
import path, { dirname } from 'path';
import { fileURLToPath } from 'url';
import GeoIP from "geoip-lite";
import requestIp from "request-ip";
import UserVisit from "./model/UserVisit.js";
import dotenv from 'dotenv'
dotenv.config()
const app = express();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Middleware
app.use(express.json());
app.use(cors());
app.use(morgan('tiny'));
app.disable('x-powered-by'); // less information about our stack
// IP tracking middleware
app.use((req, res, next) => {
const ipAddress = requestIp.getClientIp(req);
const location = GeoIP.lookup(ipAddress);
const userVisit = new UserVisit({
ipAddress: ipAddress,
location: location ? `${location.city}, ${location.country}` : 'Unknown',
timestamp: new Date()
});
userVisit.save()
.then(() => next())
.catch((error) => {
console.error('Error saving user visit:', error);
res.sendStatus(500);
});
});
const PORT = process.env.PORT || 5000;
// HTTP GET Request
// app.get('/', (req, res) => {
// res.status(201).json("Home GET Request");
// });
// API routes
app.use('/api', router);
// static files
app.use(express.static(path.join(__dirname, './client/build')));
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, './client/build/index.html'));
});
// Start server only when there is a valid connection
connect()
.then(() => {
app.listen(PORT, () => {
console.log(`Server is running on PORT:${PORT}`);
});
})
.catch((error) => {
console.log("Invalid database connection");
});