I have the below code and want to export the handler
using module.exports.handler
. Since start()
is an async function and the handler is defined inside of it, I can't export it properly by using the below code. I tried using top-level await for start() and got an error. Can I know another way to achieve this? Thanks in advance.
//index.ts
import serverless from 'serverless-http';
let handler;
const start = async (): Promise<void> => {
//some function calls with await
const server = express();
//server.use()
handler = serverless(server);
};
start().then(() => {
console.log(handler) //[AsyncFunction (anonymous)]
});
console.log(handler) //undefined
module.exports.handler = handler;
Btw, I need to use handler
in my serverless.yml
service: rest-server
provider:
name: aws
runtime: nodejs12.x
region: eu-west-1
environment: ${file(serverless.env.yml)
stage: dev
memorySize: 128
functions:
h5p:
handler: src/index.handler
events:
- http: ANY /
- http: 'ANY {proxy+}'
plugins:
- serverless-plugin-typescript
- serverless-offline