Invoke API Gateway Endpoints Locally

Link to chapter - https://serverless-stack.com/chapters/invoke-api-gateway-endpoints-locally.html

Imagine we have a path defined in serverless.yml as GET example .

So by running sls offline -s dev command, serverless-offline will create the route GET /example . But API Gateway also prepends the stage name to the path. So, API Gateway would have created GET /dev/example route.

How to achieve that using serverless-offline ?

We usually test our frontends while including the stage name in there. For example:

Hello, before all, I want to thank you all for this documentation, so helpfull!

In this chapter I got an error when I run the startServer file with the command node .\startServer.
I’m using windows for developing & I dont have idea about what Im doing wrong in this chapter. The error shows me this in the terminal:

node .\startServer
events.js:288
throw er; // Unhandled β€˜error’ event
^

Error: spawn sls ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19)
at onErrorNT (internal/child_process.js:469:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21)
Emitted β€˜error’ event on ChildProcess instance at:
at Process.ChildProcess._handle.onexit (internal/child_process.js:273:12)
at onErrorNT (internal/child_process.js:469:16)
at processTicksAndRejections (internal/process/task_queues.js:84:21) {
errno: β€˜ENOENT’,
code: β€˜ENOENT’,
syscall: β€˜spawn sls’,
path: β€˜sls’,
spawnargs: [ β€˜offline’, β€˜start’, β€˜β€“stage’, β€˜dev’, β€˜β€“port’, 3001 ]
}

How can I execute the startServer file? I need help pls.

Oh this error sounds like it’s a cross-platform issue. I don’t think this would work on Windows. A fix for this is use this package:

And use its spawn command instead.

1 Like

Hi, when I run the ./startServer command only ONE service runs. Basically, whichever service comes 2nd in the array. The 1st one exits with an error code 1 (child exited with code 1).

My Services array:

const services = [
{ route: β€œ/v3/auth/", path: β€œservices/auth”, port: 3001 },
{ route: "/v3/master/
”, path: β€œservices/master”, port: 3002 }
];

The CLI output data (if that helps)

./startServer
Serverless: Bundling with Webpack...

Serverless: Bundling with Webpack...

Serverless: Watching for changes...

offline: Starting Offline: undefined/us-east-2.

offline: Offline [http for lambda] listening on http://localhost:3002
offline: Function names exposed for local invocation by aws-sdk:
           * getMasters: master-dev-getMasters
           * createMaster: master-dev-createMaster
           * updateMaster: master-dev-updateMaster


   β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
   β”‚                                                                                β”‚
   β”‚   GET  | http://localhost:3000/dev/v3/master                                   β”‚
   β”‚   POST | http://localhost:3000/2015-03-31/functions/getMasters/invocations     β”‚
   β”‚   POST | http://localhost:3000/dev/v3/master                                   β”‚
   β”‚   POST | http://localhost:3000/2015-03-31/functions/createMaster/invocations   β”‚
   β”‚   PUT  | http://localhost:3000/dev/v3/master/{id}                              β”‚
   β”‚   POST | http://localhost:3000/2015-03-31/functions/updateMaster/invocations   β”‚
   β”‚                                                                                β”‚
   β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜


offline: [HTTP] server ready: http://localhost:3000 πŸš€

offline: 
offline: Enter "rp" to replay the last request

Serverless: Watching for changes...

offline: Starting Offline: undefined/us-east-2.

<Buffer 55 6e 65 78 70 65 63 74 65 64 20 65 72 72 6f 72 20 77 68 69 6c 65 20 73 74 61 72 74 69 6e 67 20 73 65 72 76 65 72 6c 65 73 73 2d 6f 66 66 6c 69 6e 65 ... 410 more bytes>
child exited with code 1

Thanks for any help. I am using serverless-stack examples to develop a different app. I am extremely grateful to you folks for this. This book has been my go to reference for the past 1 1/2 years. Although, my first version of my app was not serverless (backend) but heavily dependant on your frontend React chapters. Now I am moving all APIs to microservices based serverless functions when I came across this β€œstartServer” update which was not there when I first learnt from you guys. Sorry for the mostly irrelevant (for you) background :slight_smile: .

Quick update -

The service that runs seems to be random (not necessarily first or second)… When I just tried now, the other service is running not the one mentioned above.

Thanks for the kind words!

Do you know whats causing the error?

As an aside, this script needs to be improved!

Hi Jayair… thanks for your reply…

It feels like only one service can be run on a single terminal at any time. It doesn’t sound like much - but then that’s because I am new to development.

I started development just last year after 16 years in sales and I heavily depend on guides like yours to start anything new. And when that fails I am generally stuck - hehehe.

I also tried the script from this - https://towardsdatascience.com/serverless-a-painless-aws-boilerplate-e5ec3b4fb609. It is similar to your script but slightly different. Didn’t work either.

For now, I am going to run all APIs from a single yml file from the root folder for offline development. And then deploy each service separately in production. Seems like the best option for now.

For now, I am running a single lambda function from project root. I am keeping the services separate with their own yml and package.json files. Dependencies for each service are only installed under that particular service package.json. The handlers are referenced in the project root yml.

This creates too many api points. However, since I am already familiar with express like usage this seems to be fine for offline development.

Putting this here in case others come here searching for a solution (temporary though it is).

Thank you serverless-stack (and codecademy, for any new programmers out there) for helping out a new developer with no mentor available. You guys have made it possible that I am a full stack developer today. You guys rock!

1 Like

That’s awesome to hear! Glad we’ve been able to help!