sails-hook-cb-async-controller
Installation
npm i -S sails-hook-cb-async-controller
Usage
UserController.js - See this example
moduleexports = // Will crash app due to headers sent twice error { ; } // Set response.isHandled on to not send a response twice { resisHandled = true; ; } async { const users = await User; return value: userslength; } async { const users = await User; return res; } async { const users = await User; throw message: 'An error' type: 'warn'; } * { const users = User; return value: userslength; } * { const users = User; return res; } * { const users = User; throw errorCode: 1001; } { return User ; } { return User ; } { return User ; } { return value: Math; } { return res; } { throw errorCode: 1002 message: 'Error thrown' logMessage: '[throwSync] Bad error'; };
Tests
-
Add users
curl -X POST 'http://localhost:11111/users'-H 'content-type: application/x-www-form-urlencoded' -d 'name=Ren&birthyear=2005'curl -X POST 'http://localhost:11111/users' -H 'content-type: application/x-www-form-urlencoded' -d 'name=Sou&birthyear=2002' -
Test controllers with async functions
curl -X GET http://localhost:11111/users/asynccurl -X GET http://localhost:11111/users/asyncHandledcurl -X GET http://localhost:11111/users/throwAsync -
Test controllers with generator functions
curl -X GET http://localhost:11111/users/generatorcurl -X GET http://localhost:11111/users/generatorHandledcurl -X GET http://localhost:11111/users/throwGenerator -
Test controllers with functions returning promise
curl -X GET http://localhost:11111/users/promisecurl -X GET http://localhost:11111/users/promiseHandledcurl -X GET http://localhost:11111/users/throwPromise -
Test controllers with normal functions
curl -X GET http://localhost:11111/users/synccurl -X GET http://localhost:11111/users/syncHandledcurl -X GET http://localhost:11111/users/throwSync -
Test controllers with callbacks
Caution: This request will crash the app
curl -X GET http://localhost:11111/users/callbackCrashcurl -X GET http://localhost:11111/users/callbackNotCrash