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

Express body-parser doesn't parse the body #25

Closed
talentlessguy opened this issue Jul 17, 2020 · 11 comments
Closed

Express body-parser doesn't parse the body #25

talentlessguy opened this issue Jul 17, 2020 · 11 comments
Labels
bug Something isn't working core Affects `@tinyhttp/app` - the tinyhttp core

Comments

@talentlessguy
Copy link
Member

talentlessguy commented Jul 17, 2020

Describe the bug

With using Express body-parser, neither bodyParser.json nor bodyParser.urlencoded work. Instead of a body will with data we get {}

To Reproduce
Steps to reproduce the behavior:

import { App } from '@tinyhttp/app'
import bodyParser from 'body-parser'

const app = new App()

app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json

app.use(bodyParser.json())

app.use(function (req, res) {

  res.setHeader('Content-Type', 'text/plain')
  res.write('you posted:\n')
  res.end(JSON.stringify(req.body, null, 2))

})

app.listen(3000)

and then in terminal:

curl localhost:3000 -X POST -F "a=b"

Expected behavior

req.body would be filled with data

Versions

  • node: 14.5.0
  • @tinyhttp: 0.2.12

Additional context

body-parser repo

@talentlessguy talentlessguy added bug Something isn't working core Affects `@tinyhttp/app` - the tinyhttp core labels Jul 17, 2020
@talentlessguy
Copy link
Member Author

Temporary workaround: using parsec

@talentlessguy
Copy link
Member Author

After some magic happening, it now works:

import { App } from '@tinyhttp/app'
import bodyParser from 'body-parser'

const app = new App()

app.use(bodyParser.urlencoded({ extended: false }))

app.use((req, res, next) => {
    console.log(req.body)
    next()
})

app.get('/', (req, res) => void res.send(req.body))

app.listen(3000)

@spideythewebhead
Copy link

Hi, i tried to use body parser with tinyhttp (using typescript) but it doesnt seem to work due to types

@talentlessguy
Copy link
Member Author

@spideythewebhead please provide a reproduction sample

@spideythewebhead
Copy link

Holy shit you answered fast :p

import { App } from "@tinyhttp/app";
import { logger } from "@tinyhttp/logger";
import bodyParser from "body-parser";

export const server = new App();

server.use(bodyParser.json());

Screenshot 2021-02-17 at 10 57 01 PM

@spideythewebhead
Copy link

ok, a 'workaround' is to cast as any and seems to work fine!

server.use(bodyParser.json() as any);
server.use(bodyParser.urlencoded() as any);

Sorry, for the inconvenience.

@talentlessguy
Copy link
Member Author

@spideythewebhead that's not a good solution

could you pls provide your tinyhttp version? I thought that type error got fixed before

@spideythewebhead
Copy link

spideythewebhead commented Feb 18, 2021

@talentlessguy yeah i know but its not a great deal.
I just run npm install @tinyhttp/app, on a new project.

"@tinyhttp/app": "^1.1.12",
"body-parser": "^1.19.0"

@spideythewebhead
Copy link

Ok I am not sure what exactly happened, I did try it on a new project and it works without the casting

@iamricard
Copy link

iamricard commented Mar 8, 2021

Hi! I've run into this, and it seems to still be happening. body-parser@1.19.0 (same for @types) and @tinyhttp/app@1.1.12. It looks like SyncHandler and AsyncHandler claim that next?: NextFunction is nullable, but body-parser types expect it to always be present export type NextHandleFunction = (req: IncomingMessage, res: http.ServerResponse, next: NextFunction) => void;. A quick scan of the body-parser source makes me think that they in fact expect next to always be present.

@talentlessguy
Copy link
Member Author

@iamricard Cannot reproduce this, body-parser here even with installed types works:

e8bc094

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working core Affects `@tinyhttp/app` - the tinyhttp core
Projects
None yet
Development

No branches or pull requests

3 participants