Run lambda functions with a http server.
If you have the following directory structure and files, you can simply import lambda-server
and start it.
The lambda-server
expects to parameters: a directory and a port.
.
├── lambda
│ ├── foo
│ │ └── bar.js
│ └── hello-world.js
└── index.js
$ npm -g i @nonpolynomial/lambda-server
# or
$ yarn add @nonpolynomial/lambda-server
// index.js
const lambda = require('lambda');
const lambdaServer = lambda(`${__dirname}/lambda`, 8080);
// lambda/foo/bar.js
module.exports = (req, res) => {
res.end(JSON.stringify({ foo: 'bar' }));
};
// lambda/hello-world.js
module.exports = (req, res) => {
res.end('hello world');
};
Because we started lambda-server
with lambda
as directory, you get a hello world
if you visit http://localhost:8080/hello-world.
The file, that gets loaded, depends on the path you visit.
URL-path | loaded filepath |
---|---|
/foo/bar | foo/bar.js |
/hello-world | hello-world.js |
npm -g i @nonpolynomial/lambda-server
# or
yarn global add @nonpolynomial/lambda-server
$ lambda ./lambda --port 1337