-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.ts
41 lines (36 loc) · 951 Bytes
/
app.ts
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
import * as express from "express";
import type { ErrorRequestHandler } from "express";
import fetch from "node-fetch";
export const createApp = (service: string, version: string) => {
const app = express();
app.use(express.json());
app.use("/downstream/:service", async (req, res, next) => {
const { service: downstreamService } = req.params;
try {
console.log(`Fetching downstream: ${downstreamService}`);
const response = await fetch(`http://${downstreamService}`);
res.json({
message: "fetched!",
service,
response: await response.json(),
});
} catch (err) {
next(err);
}
});
app.use("/", (req, res) => {
res.json({
message: "ok",
service,
version,
});
});
app.use(((err, req, res, next) => {
res.status(500).json({
message: "error",
service,
error: err,
});
}) as ErrorRequestHandler);
return app;
};