xebia / mock-rest-request

Mock REST requests in NodeJS
https://xebia.com/blog/mocking-a-rest-backend-for-your-angularjs-grunt-web-application/
Apache License 2.0
12 stars 6 forks source link

Server crashes (sometimes) when unregistered endpoint requested #6

Open tommcintyre opened 8 years ago

tommcintyre commented 8 years ago

When my application requests an unregistered endpoint, the mock server crashes:

path/to/mock-server/node_modules/mock-rest-request/index.js:60
        next();
        ^

TypeError: next is not a function
    at path/to/mock-server/node_modules/mock-rest-request/index.js:60:9
    at Server.<anonymous> (path/to/mock-server/app.js:8:17)
    at emitTwo (events.js:100:13)
    at Server.emit (events.js:185:7)
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:537:12)
    at HTTPParser.parserOnHeadersComplete (_http_common.js:103:23)

[1]+  Exit 1                  node app.js

The mock server doesn't crash when I request an unregistered endpoint using curl; only when called from my app, which uses restling. Perhaps restling closes the connection down more quickly than curl, or something...?

tommcintyre commented 8 years ago

FYI, I have mock-rest-request@0.0.3. My index.js:60 is actually line 82 in the current version in master. It's the final next() call in this part of the code:

      var mockedResponse = mocks[req.method][req.url];
      if (mockedResponse) {
        res.writeHead(mockedResponse.responseCode, mockedResponse.headers);
        res.write(mockedResponse.body);
        res.end();
      } else {
        next();
      }