Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mock server is not receiving the full request anymore #375

Closed
paraita opened this issue Sep 29, 2016 · 4 comments
Closed

mock server is not receiving the full request anymore #375

paraita opened this issue Sep 29, 2016 · 4 comments

Comments

@paraita
Copy link

paraita commented Sep 29, 2016

Hi,

I use json-server to mock some authentication endpoint, with the following code:

var jsonServer = require('json-server');
var express = require('express');
var server = jsonServer.create();
var middlewares = jsonServer.defaults();
server.use(middlewares);

server.post('/rest/my/login-endpoint', function (req, res, next) {
    console.log('LOGIN');
    var data = "";
    req.on('data', function(chunk) {
        console.log("Received body data:");
        console.log(chunk.toString());
        data += chunk.toString();
    });
    req.on('end', function() {
        console.log('body:');
        console.log(data);

        // We assume the request is well formed
        var pairs = data.split('&');
        var username = pairs[0].split('=')[1];
        var password = pairs[1].split('=')[1];

        // check the user/pass
        var statusCode = 404;
        if (username == 'user' && password == 'pwd') {
            console.log('login successful');
            statusCode = 200;
        }
        res.status(statusCode);
        res.setHeader('content-type', 'application/json');
        res.write('MOCKED_SESSION_ID');
        res.end();
    });
});

server.use(express.static('public'));
server.listen(8080, function () {
    console.log('JSON Server is running');
});

On 0.8.14, the following curl command works:
curl --request POST --url http://localhost:8080/rest/my/login-endpoint --header 'content-type: application/x-www-form-urlencoded' --data 'username=dazdaz&password=pwd'

The log on json-server are:

node test/json-server-data/mock-scheduler-rest.js
JSON Server is running
LOGIN
Received body data:
username=dazdaz&password=pwd
body:
username=dazdaz&password=pwd
POST /rest/studio/login 404 7.344 ms - -
LOGIN
Received body data:
username=dazdaz&password=pwd
body:
username=dazdaz&password=pwd
POST /rest/studio/login 404 2.160 ms - -
^C

But it doesn't work anymore on 0.8.21, as the curl seems to hang indefinitely, and I have the following log on json-server:

node test/json-server-data/mock-scheduler-rest.js                                                                                                                 
JSON Server is running
LOGIN


I am using:

  • npm 3.10.8
  • nodejs 6.7.0
  • Archlinux
@typicode
Copy link
Owner

typicode commented Oct 5, 2016

Hi @paraita,

Thanks for the detailed code 👍 . I tried with 0.8.20 and it works.

In 0.8.21, body-parser middleware was included in jsonServer.defaults() but it doesn't seem work well in some cases #363.

I've removed it in next branch but it's not yet ready to be published. Until then, I would suggest sticking to 0.8.20.

@paraita
Copy link
Author

paraita commented Oct 7, 2016

Thank you @typicode for your answer, I'll stick with 0.8.20 for now :)

@typicode
Copy link
Owner

Closing this issue as it's a duplicate of #363
Also I've published v0.9.0-beta.2 which should solve this issue.

You can install it using npm install json-server@next.
Let me know if you spot any issue :)

@paraita
Copy link
Author

paraita commented Jan 19, 2017

Thanks I've updated successfully to 0.9.0 and 9.4 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants