-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
58 lines (45 loc) · 1.45 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
const express = require('express');
const dbController = require('./db')
const cosmeticRouter = require('./cosmetic.controller')
require('dotenv').config();
const app = express();
// Middleware to allow only specified IP addresses
const allowOnlyIPs = (req, res, next) => {
const allowedIPs = process.env.ALLOWED_IPS.split(',');
let clientIP = req.headers['x-forwarded-for'] || req.ip;
if (clientIP.startsWith('::ffff:')) {
clientIP = clientIP.slice(7); // Remove the '::ffff:' prefix
}
if (allowedIPs.includes(clientIP)) {
// IP is allowed, proceed to the next middleware or route handler
next();
} else {
// IP is not allowed, send forbidden response
res.status(403).send('Forbidden');
}
};
app.use(allowOnlyIPs);
app.use('/api/cosmetics', cosmeticRouter.router);
// Connect to MySQL
dbController.getConnection.connect((err) => {
if (err) {
throw err;
}
console.log('Connected to MySQL database');
dbController.createTables();
});
// Define a route to fetch data from MySQL
// app.get('/cosmetics', (req, res) => {
// const query = 'SELECT * FROM cosmetics';
// dbController.getConnection.query(query, (err, results) => {
// if (err) {
// throw err;
// }
// res.json(results);
// });
// });
// Start the server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Server started on port ${port}`);
});