user1592380
user1592380

Reputation: 36317

Context and module.exports in serverless framework functions

enter image description here

I'm getting started with nodejs and the serverless framwork. in some of the examples i've seen and even used the following code (from https://www.serverless.com/framework/docs/providers/aws/guide/functions/):

// handler.js
module.exports.functionOne = function(event, context, callback) {};

2 questions:

1) does module.exports have anything to do with the common node method of making functions available to other nodules?

2)What is the context they are referring to here? based on http://ryanmorr.com/understanding-scope-and-context-in-javascript/

I see:

Every function invocation has both a scope and a context associated with it. Fundamentally, scope is function-based while context is object-based. In other words, scope pertains to the variable access of a function when it is invoked and is unique to each invocation. Context is always the value of the this keyword which is a reference to the object that “owns” the currently executing code.

Does this apply here?

Upvotes: 0

Views: 811

Answers (1)

Ville Rinne
Ville Rinne

Reputation: 821

The export is standard node.js, this is what maps your function implementation to the function declaration inside your serverless.yml

The context object is the AWS Lambda context (https://docs.aws.amazon.com/lambda/latest/dg/nodejs-context.html). If you look closely the example you linked is for AWS, the signature for the handler will be different for other cloud platforms.

Upvotes: 1

Related Questions