This repository has been archived by the owner on Feb 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathserver.js
79 lines (73 loc) · 2.48 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
71
72
73
74
75
76
77
78
79
const next = require("next");
const express = require("express");
const port = parseInt(process.env.FRONTEND_PORT, 10) || 3000;
const { createProxyMiddleware } = require("http-proxy-middleware");
const dev = process.env.NODE_ENV !== "production";
const app = next({ dev });
const handler = app.getRequestHandler();
const sourcemapsForSentryOnly = (token) => (req, res, next) => {
// In production we only want to serve source maps for Sentry
if (!dev && !!token && req.headers["x-sentry-token"] !== token) {
res
.status(401)
.send(
"Authentication access token is required to access the source map."
);
return;
}
next();
};
const faultyRoute = () => {
throw new Error("Server exception");
};
app.prepare().then(() => {
// app.buildId is only available after app.prepare(), hence why we setup here
const { Sentry } = require("./src/sentry")(app.buildId);
express()
// This attaches request information to Sentry errors
.use(Sentry.Handlers.requestHandler())
.use(
createProxyMiddleware("/graphql", {
target: process.env.GRAPHQL_ENDPOINT,
changeOrigin: true,
xfwd: true,
prependPath: false,
followRedirects: true,
pathRewrite: { "^/graphql": "/v1/graphql" },
logLevel: "debug",
onError: (err, req, res) => {
res.writeHead(500, {
"Content-Type": "text/plain",
});
// todo: sentry
res.end("Something went wrong. We've been notified.");
},
ws: true, // proxy websockets
})
)
.get(/\.map$/, sourcemapsForSentryOnly(process.env.SENTRY_TOKEN))
// demo server-exception
.get("/page-error", faultyRoute)
// Regular next.js request handler
.use(handler)
// This handles errors if they are thrown before reaching the app
.use(Sentry.Handlers.errorHandler())
.listen(port, (err) => {
if (err) {
throw err;
}
console.log("process.env.SENTRY_DSN", process.env.SENTRY_DSN);
console.log("process.env.SENTRY_TOKEN", process.env.SENTRY_TOKEN);
console.log(
"process.env.NEXT_PUBLIC_MATOMO_URL",
process.env.NEXT_PUBLIC_MATOMO_URL
);
console.log(
"process.env.NEXT_PUBLIC_MATOMO_SITE_ID",
process.env.NEXT_PUBLIC_MATOMO_SITE_ID
);
console.log("process.env.GRAPHQL_ENDPOINT", process.env.GRAPHQL_ENDPOINT);
// eslint-disable-next-line no-console
console.log(`> Ready on http://localhost:${port}`);
});
});