An express wrapper which allows you to quickly and dynamically create api endpoints, without having to worry about boilerplate code.
npm i @iobxt/api-creator
Create a directory structure, something like this:
api
|- foo
|- doSomething.js
Your api endpoints must look something like this:
// doSomething.js
const init = (router, config, resources) => {
router.get('/hello', (req, res) => res.send('hello'));
};
module.exports = { init };
Initialise the library with something like this:
const apiCreator = require('api-creator');
const path = require('path');
const apiPath = path.resolve('./api/');
apiCreator.init(apiPath, config, resources);
Your endpoints will be accessible on:
Create a lambda.js
file and export the module like so;
const api = apiCreator.init(apiPath, config, resources);
module.exports = api.handler;
You can control the creator behaviour with the following environment varibles:
Name | Description | Default value | Possible values |
---|---|---|---|
APP_PORT | the port to listen on | 8080 | |
PLATFORM | How the api runs | development | development, lambda |
You can provide endpoint specific configuration by passing in a config
param.
const config = {
corsOptions: {
origin: ['http://localhost:3000']
},
bodyParserEnabled: true,
foo: {
some: "config";
}
}
This will configure everything under the foo
directory.
- Commit changes
- Run
npm publish:version major|minor|patch
- Profit