node.jstotal.js

How to find out which get and post request called in same function handler?


According to this example:

ROUTE('GET /api/users/', action);
ROUTE('POST /api/users/', action);

in the body of function action how we can find out get or post request has been called so that write appropriate code?


Solution

  • You might pass a function that calls action with another argument that indicates which method was used:

    ROUTE('GET /api/users/', function(...args) { action.call(this, 'GET', ...args) });
    ROUTE('POST /api/users/', function(...args) { action.call(this, 'POST', ...args) });
    

    With this, the first argument passed to action will be the method used, and the rest of the arguments will be the ones that the ROUTE callback would receive normally.