|
| 1 | +import express from 'express'; |
| 2 | +import 'dotenv/config'; |
| 3 | + |
| 4 | +import worker from '../index.mjs'; |
| 5 | +import getEnv from './env-binding.mjs'; |
| 6 | + |
| 7 | +const port = process.env.PORT ?? 8787; |
| 8 | + |
| 9 | +const convertIncomingMessageToRequest = (req) => { |
| 10 | + var headers = new Headers(); |
| 11 | + for (var key in req.headers) { |
| 12 | + if (req.headers[key]) headers.append(key, req.headers[key]); |
| 13 | + } |
| 14 | + let body = req.body; |
| 15 | + if (typeof body === 'object') { |
| 16 | + body = JSON.stringify(body); |
| 17 | + } |
| 18 | + let request = new Request(new URL(req.url, `http://localhost:${port}`).toString(), { |
| 19 | + method: req.method, |
| 20 | + body: req.method === 'POST' ? body : null, |
| 21 | + headers, |
| 22 | + }) |
| 23 | + return request |
| 24 | +}; |
| 25 | + |
| 26 | +const app = express(); |
| 27 | +app.use(express.json({limit: '100mb'}), express.text()); |
| 28 | +app.all('*', async (req, res, next) => { |
| 29 | + const response = await worker.fetch(convertIncomingMessageToRequest(req), getEnv(), {waitUntil: () => {}}); |
| 30 | + |
| 31 | + // Convert Response object to JSON |
| 32 | + const responseBody = await response.text(); |
| 33 | + |
| 34 | + // Reflect headers from Response object |
| 35 | + //Object.entries(response.headers.raw()).forEach(([key, value]) => { |
| 36 | + response.headers.forEach((value, key) => { |
| 37 | + res.setHeader(key, value); |
| 38 | + }); |
| 39 | + |
| 40 | + // Send the status and JSON body |
| 41 | + res.status(response.status).send(responseBody); |
| 42 | +}); |
| 43 | + |
| 44 | +app.listen(port, () => { |
| 45 | + console.log(`HTTP GraphQL server running at http://localhost:${port}`); |
| 46 | +}); |
0 commit comments