This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (50 loc) · 1.67 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
require('dotenv').config();
const express = require('express');
const cors = require('cors');
const postListRoutes = require('./features/post-list/post-list-rest');
const postDetailsRoutes = require('./features/post-list/post-details-rest');
const cocktailDetails = require('./features/cocktail/rest');
const service = require('./features/service');
const glasswareDetails = require('./features/glassware/rest');
const toolDetails = require('./features/tools/rest');
const goodDetails = require('./features/goods/rest');
const filter = require('./features/filters/rest');
const siteMap = require('./features/sitemap/rest');
const { checkIsUrlNeedRedirect } = require('./features/redirects/check');
const app = express();
const port = process.env.APP_PORT;
app.use(cors({
origin: '*', // Equivalent to anyHost()
methods: '*', // Equivalent to allowing all methods
allowedHeaders: '*', // Equivalent to allowHeaders { true }
credentials: true, // Equivalent to allowCredentials = true
preflightContinue: false,
optionsSuccessStatus: 204,
}));
app.use(express.json());
async function redirectIfXUserPath(req, res, next) {
const xUserPath = req.headers['x-user-path'];
if (xUserPath) {
const redirect = await checkIsUrlNeedRedirect(xUserPath)
if (redirect.isRedirect == true) {
return res.status(200).json({ redirect: redirect.to });
}
}
next();
}
// Use the middleware in your Express app
app.use(redirectIfXUserPath);
app.use(
postListRoutes,
postDetailsRoutes,
cocktailDetails,
glasswareDetails,
toolDetails,
goodDetails,
filter,
siteMap,
service
);
app.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});