Skip to content

Commit 9cc1f94

Browse files
committed
Handle missing URLs, process originalUrl
1 parent 9493783 commit 9cc1f94

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/index.js

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,45 @@ const app = combineMiddlewares([
1313
*
1414
* This is typically useful for augmenting the request before it goes to PostGraphile.
1515
*/
16+
(req, res, next) => {
17+
if (options.absoluteRoutes) {
18+
try {
19+
const event = JSON.parse(decodeURIComponent(req.headers['x-apigateway-event']));
20+
// This contains the `stage`, making it a true absolute URL (which we
21+
// need for serving assets)
22+
const realPath = event.requestContext.path;
23+
req.originalUrl = realPath;
24+
} catch (e) {
25+
next(new Error('Processing event failed'));
26+
}
27+
}
28+
next();
29+
},
1630
postgraphile(process.env.DATABASE_URL, schemas, {
1731
graphqlRoute: '/',
1832
...options,
1933
readCache: `${__dirname}/postgraphile.cache`,
2034
}),
2135
]);
2236

37+
const handler = (req, res) => {
38+
app(req, res, err => {
39+
if (err) {
40+
// eslint-disable-next-line no-console
41+
console.error(err);
42+
res.writeHead(err.status || err.statusCode || 500);
43+
res.end(err.message);
44+
return;
45+
}
46+
if (!res.finished) {
47+
if (!res.headersSent) {
48+
res.writeHead(404);
49+
}
50+
res.end(`'${req.url}' not found`);
51+
}
52+
});
53+
};
54+
2355
const binaryMimeTypes = options.graphiql ? ['image/x-icon'] : undefined;
24-
const server = awsServerlessExpress.createServer(app, undefined, binaryMimeTypes);
56+
const server = awsServerlessExpress.createServer(handler, undefined, binaryMimeTypes);
2557
exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context);

0 commit comments

Comments
 (0)