smollweide / node-mock-server

File based Node REST API mock server
MIT License
255 stars 65 forks source link

Request: set HTTP statusCode with EJS #109

Closed BuruY closed 6 years ago

BuruY commented 6 years ago

Hi @smollweide, Thank you for maintaining this package!

I have following succes.json:

<%
    var data = {};
    switch (query.Name) {  
        case 'noresults':
            data = response['error-204'];
            break;
        default:
            data = response['success_default'];
            break;
}%>

<%-JSON.stringify(data);%>

Here I would like when Name = noresults, it returns a 204 HTTP status code. But it seems (since this logic is applied from the success.json) always returning a statusCode 200.

When calling the file error-204 directly with response.txt it does show correct statusCode, but this makes it no longer dynamic (input based).

Is there another way node-mock-server supports my use case?

smollweide commented 6 years ago

Hi @BuruY, thank you for creating this issue. For more complex scenarios like this one you should use the middleware.

Best, Simon

BuruY commented 6 years ago

Thanks for the lightning fast response.

I have following code as middleware:

    middleware: {
        '/rest/xxx/#xxx/GET/mock'(serverOptions, requestOptions) {
            var name = requestOptions.req.query.Name;
            console.log('TEST' + name);

            if (name === 'noresults') {
                requestOptions.res.statusCode = 204;
                return null;
            }

            return 'success';
        }
    }

I do not seem to do it right, I have found no query examples in pointed documentation. Perhaps you can point me towards correct direction?

Thanks!

BuruY commented 6 years ago

Got it to work, documentation is not clear on this one but you need to have a response.txt that states: middleware