GithubHelp home page GithubHelp logo

Comments (6)

dead-horse avatar dead-horse commented on May 3, 2024

dose this meet your needs?

from bodyparser.

pensierinmusica avatar pensierinmusica commented on May 3, 2024

@dead-horse, I tried a similar solution before, but probably I did something wrong since it returns an error.

Here's an example of the code:

var app = require('koa')();
var router = require('koa-router')();
var bodyParser = require('koa-bodyparser');

router.post('/', function* () {
  yield bodyParser();
  this.body = this.request.body;
});

app.use(router.routes());

app.listen(3000);

And the error:

TypeError: Cannot read property 'Symbol(Symbol.iterator)' of undefined at Object.bodyParser

Could you please help me in figuring out what I'm doing wrong / how to fix it?

Thanks!

from bodyparser.

dead-horse avatar dead-horse commented on May 3, 2024
var koa = require('koa')
var router = require('koa-router')()
var bodyparser = require('koa-bodyparser')

var app = koa()

router.post('/', bodyparser(), function* () {
  this.body = this.request.body
})

app.use(router.routes()).listen(3000)

from bodyparser.

pensierinmusica avatar pensierinmusica commented on May 3, 2024

@dead-horse, I see, thanks!

Could you help me understand a bit better what is the difference between my initial version and your version, or point me in the right direction?

Also, I'd prefer "yielding" the body parser inside a generator function, so I can choose exactly when to do it (for example, after checking some headers). The approach is similar to the one shown here, using koa-body-parsers, where you can do something like:

app.use(function* (next) {
  // Some code
  var body = yield this.request.json();
  // Some other code
  yield next;
});

Is there any way to achieve this with koa-bodyparser?

from bodyparser.

dead-horse avatar dead-horse commented on May 3, 2024

bodyparser() return a koa middleware, so if you want to "yielding" the body parser inside a generator function, you can just use co-body

from bodyparser.

pensierinmusica avatar pensierinmusica commented on May 3, 2024

Sure, that makes sense, thanks!

from bodyparser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.