Skip to content

Commit 02938da

Browse files
committed
update fastify example
1 parent d360458 commit 02938da

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

fastify-app-lambda/README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ This is a simple example that shows how to deploy an existing [fastify](https://
55
## Running the example
66

77
1. run `npm install` to grab the dependencies
8-
2. run `npm run generate-proxy` to create a simple proxy API for the fastify app
9-
3. run `npm run deploy` to send everything up to AWS Lambda
8+
2. run `npm run deploy` to send everything up to AWS Lambda
109

1110
The third step will print out a URL you can use to access the fastify app.
1211

fastify-app-lambda/app.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
const fastify = require('fastify')();
1+
const fastify = require('fastify');
22

3-
fastify.get('/', (request, reply) => reply.send({ hello: 'world' }));
3+
function init(serverFactory) {
4+
const app = fastify({ serverFactory });
5+
app.get('/', (request, reply) => reply.send({ hello: 'world' }));
6+
return app;
7+
}
48

59
if (require.main === module) {
610
// called directly i.e. "node app"
7-
fastify.listen(3000, (err) => {
11+
init().listen(3000, (err) => {
812
if (err) console.error(err);
9-
console.log(`server listening on ${fastify.server.address().port}`);
13+
console.log('server listening on 3000');
1014
});
1115
} else {
1216
// required as a module => executed on aws lambda
13-
module.exports = fastify;
17+
module.exports = init;
1418
}

fastify-app-lambda/lambda.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const awsServerlessExpress = require('aws-serverless-express');
2+
const fastify = require('./app');
3+
let server;
4+
const serverFactory = (handler) => {
5+
server = awsServerlessExpress.createServer(handler);
6+
return server;
7+
}
8+
const app = fastify(serverFactory);
9+
exports.handler = (event, context, callback) => {
10+
context.callbackWaitsForEmptyEventLoop = false;
11+
app.ready((e) => {
12+
if (e) return console.error(e.stack || e);
13+
awsServerlessExpress.proxy(server, event, context, 'CALLBACK', callback);
14+
});
15+
};

fastify-app-lambda/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
},
1111
"license": "Apache-2.0",
1212
"dependencies": {
13-
"aws-serverless-express": "^3.0.2",
14-
"fastify": "^0.24.0"
13+
"aws-serverless-express": "3.3.6",
14+
"fastify": "2.1.0"
1515
},
1616
"devDependencies": {
17-
"claudia": "^4"
17+
"claudia": "^5"
1818
}
1919
}

0 commit comments

Comments
 (0)