forked from jefersonhuan/datadog-proxy-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
62 lines (50 loc) · 1.56 KB
/
app.js
File metadata and controls
62 lines (50 loc) · 1.56 KB
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
const express = require('express');
const multer = require('multer');
const querystring = require('querystring');
const axios = require('axios');
const winston = require('winston');
const cors = require('cors');
// Initialize Express app
const app = express();
// Define the hostname
const ddhostname = process.env.ddhostname || "browser-intake-datadoghq.com";
app.use(cors())
// Winston logger setup
const logger = winston.createLogger({
level: 'info',
format: winston.format.prettyPrint(),
transports: [
new winston.transports.Console()
]
});
// Configure Multer (adjust storage and limits as needed)
const upload = multer();
app.get('/health', (_, res) => res.sendStatus(200));
// The main POST route
app.post('/', (req, res) => {
const forwardPath = decodeURIComponent(req.query.ddforward);
const queryParams = {...req.query};
delete queryParams.ddforward;
const baseUrl = `https://${ddhostname}`;
const fullUrl = `${baseUrl}${forwardPath}?${querystring.stringify(queryParams)}`;
axios.post(fullUrl, req, {
headers: {
...req.headers,
'host': ddhostname,
},
})
.then(response => {
res.status(response.status).send(response.data);
})
.catch(error => {
const errorDetails = {
message: error.message,
status: error.response?.status,
statusText: error.response?.statusText,
responseBody: error.response?.data
};
logger.error('Error occurred', errorDetails);
res.status(error.response?.status || 500).send(error.response?.data || 'Internal Server Error');
});
});
module.exports = app;