Node.js ExpressJS API Handler
simple-api-express is an ExpressJS api handler (NOT middleware) that work with simple-api-client-ng2, an Angular api service.
Install
|
|
Usage Flow
simple-api-express
depends on ExpressJS middleware bodyParser for json body decode.
|
|
Import simple-api-express
:
|
|
Create api object with base url:
|
|
Register a function as api callback:
|
|
You can also register all functions of an object as api callbacks:
|
|
Use express post and SimpleApi.response()
to handle incoming api request:
|
|
SimpleApi.handler()
can be use if additional action(eg: customizing response header or error page) is required before reply:
|
|
API
constructor
SimpleApi(baseUrl:string, debug:boolean)
baseUrl
will prefix all api url registered to this SimpleApi instance.debug
will enable/disable debug log. Default to false.
|
|
debug
debug(enable: boolean)
can enable/disable debug log.
|
|
list
list()
return a string[]
containing all registered api url.
|
|
Output:
|
|
register
register(url:string,callback)
register a callback function to url
url
: Api url path after baseUrl. The resulting url for the api is baseUrl/url.callback
: a function that take a single argument as api parameter, and return a result.
|
|
registerObject
registerObject(object)
register all functions of an object as api callbacks.
All functions of the object should take a single argument as api parameter, and return a result.
The function name will be used api url.
|
|
response
SimpleApi.response(req, res)
is a handle function for incoming api post request. Api parameter will be passed to corresponding callback. Callback result will be passed back to api client.
req, res
are request and response object pass in from ExpressJs post.
|
|
handler
SimpleApi.handler(req)
is an api handler function. It will invoke the corresponding callback base on the request url, and return the result.
IT WILL NOT send out the result.
IT IS NOT a ExpressJs post handler function. It needed to be called INSIDE the post handler function.
Api handler can be use if additional action(eg: customizing response header or error page) is required:
|
|
Error Handling
There are two types of error.
404 Not Found
When response()
is called with an non-exist api url, it will response with a HTTP 404 Not Found.
When handle()
is called with an non-exist api url, it will throw an error, which can be caught in the post handle function.
Callback throw
When response()
is called, and the invoked api callback throw an error, which will be passed to remote client.
The remote client, using simple-api-client-ng2, will throw an exception with the error.
When handle()
is called, and the invoked api callback throw an error, the error can be inspected from the result object.
|
|
The remote client, using simple-api-client-ng2, will throw an exception with the error.
Example
A detail example for how simple-api-express and simple-api-client-ng2 work is in:
Repository
Contributors
Changelog
- 1.2.0
- Publish to NPM.
- 1.2.1
- Fix Readme.md typo
- 1.2.2
- Update package.json
- Update Readme.md
- 4.0.0
- Match ExpressJS major version
- README.md clean up
- package.json clean up
- simple-api.ts uses typescript import
- 5.0.0
- Move target from ES5 to ES2015
License
The MIT License
Copyright (c) 2021
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.