micro-dev icon indicating copy to clipboard operation
micro-dev copied to clipboard

Parsing json hangs

Open snewell92 opened this issue 8 years ago • 15 comments

I had been using micro perfectly fine locally, but then decided to use micro-dev for local development, but it won't parse the json body of my requests anymore :\ I'm sending the requests via postman. Not sure what's different or if I missed something in the docs/setup. (I didn't change source code at all).

Gist of code

The important part of my gist is index.ts, and the function waitForEmail line 75

Versions Node: 8.4.0 and 8.5.0 NPM: 5.3.0 and 5.4.2 OS: WIndows 10 Pro and Ubuntu (WSL); both 64bit

(package.json given in gist, shows latest versions of micro and micro-dev).

Thanks for reading!

snewell92 avatar Oct 04 '17 17:10 snewell92

I have the same issue. Parsing buffer, text or json causes micro-dev to hang.

The problem lies in log.js - reqBodyReady needs to be set after res.end

https://github.com/zeit/micro-dev/blob/11e501734daf2f02896321d2321e1e03a8b752ad/lib/log.js#L97

maccyber avatar Oct 24 '17 09:10 maccyber

@maccyber I think the real problem here is that logRequest does not share same request body cache with loaded micro instance.

floatdrop avatar Oct 24 '17 11:10 floatdrop

Ah, that may be @floatdrop

Example with router and express-jwt

const {send, json} = require('micro')
const Router = require('router')
const finalhandler = require('finalhandler')
const jwt = require('express-jwt')
const router = Router()

const test = async (req, res) => {
  try {
    const payload = await json(req)
    send(res, 200, payload)
  } catch (error) {
    send(res, 500, error)
  }
}

router.use(jwt({secret: 'abc123'}))
router.post('/', test)

module.exports = (req, res) => {
  router(req, res, finalhandler(req, res))
}

Request

curl -i -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.9anRBxf5MRx0vhC4oUlZoVlqL7IZBBvuEfcAJP1VUtI"  -d '{"test":1}' http://localhost:3000/

maccyber avatar Oct 24 '17 11:10 maccyber

await json(req) hangs for me too.

varna avatar Nov 07 '17 10:11 varna

Has this been fixed? Anyone have a solution?

NetOpWibby avatar Jan 21 '18 21:01 NetOpWibby

Still having the same problem.

lilonpro avatar Mar 12 '18 17:03 lilonpro

This still hasn't been fixed and I'm kind of stuck on it. :(

kdojeteri avatar May 14 '18 13:05 kdojeteri

Same for me

ezequieltejada avatar May 28 '18 20:05 ezequieltejada

I just stumbled upon the same problem again. Is micro dying? 🗡

varna avatar Aug 30 '18 12:08 varna

looks like it is.

lilonpro avatar Aug 30 '18 12:08 lilonpro

Time for a fork!

NetOpWibby avatar Aug 30 '18 14:08 NetOpWibby

Pull requests are welcome btw 🤞

jamo avatar Aug 30 '18 14:08 jamo

There are a couple PRs with no comments or indication that they'll be merged. Where's the incentive?

NetOpWibby avatar Aug 30 '18 17:08 NetOpWibby

Any plans on tending to this issue?

ferdinandsalis avatar Jun 25 '19 15:06 ferdinandsalis

I think the simpliest turnaround would be not use micro-dev as global dependency. Just install it as devDependency on the project and add a script.

$ npm install --save-dev micro-dev

// package.json
{
  "scripts": {
    "dev": "micro-dev index.js"
  }
}

$ npm run dev

The definitive solution doesn't look simple since it would be needed to read request stream twice, or clone it somehow.

cchiaramelli avatar Jun 04 '20 04:06 cchiaramelli