helapkg / hela

:icecream: Powerful software development experience and management. Enhancing @tc39 JS, @denoland and @nodejs, because we need a bit of magic. :sparkles: You can think of it as Cargo for the JavaScript ecosystem.
Mozilla Public License 2.0
331 stars 41 forks source link

Cann‘t parse json #79

Closed SteveCruise closed 7 years ago

SteveCruise commented 7 years ago

My code

const http = require('http');
const https = require('https');
const fs = require('fs');
const path = require('path');
const url = require('url');
const util = require('util');
const Koa = require('koa');
const enforceHttps = require('koa-sslify');
const favicon = require('koa-favicon');
const serve = require('koa-static');
const logger = require('koa-logger');
const bodyParser = require('koa-better-body');
const session = require('koa-session-minimal');
const redisStore = require('koa-redis');
const handleError = require('koa-handle-error');
const render = require('koa-ejs');

const app = new Koa();
render(app, {
    root: path.join(__dirname, 'views'),
    layout: false,
    viewExt: 'ejs',
    cache: false,
    debug: true
});
app.use(logger());
app.use(enforceHttps());
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(serve(__dirname + '/public'));
app.use(bodyParser());
app.use(async (ctx, next) => {
    return next();
});

I set breakpoint behind app.use(bodyParser());, ctx.request.fields is still null.

tunnckoCore commented 7 years ago
  1. Please use correct syntax highlighting
  2. There are tons of actual tests, battle-tested in production for years. How you understood that it can't parse json?
  3. You don't have any other middleware that can be conflicting with the body parsing and koa-better-body, so not make sense.
  4. Try to console.log at the last middleware console.log(ctx.request.fields)
  5. Read the contributing guide, at least - Details of Highly Effective Bug Reports
SteveCruise commented 7 years ago

@tunnckoCore Thank you very very much, I am sorry I am unskilled , at last I choose koa-bodyparser instead. Thank you for your answer. koa-better-body is a great project. If I use it again, I will solve it.

tunnckoCore commented 7 years ago

@SteveCruise yea, it's better to use it if you don't need so rich support and more specifically multipart.

koa-better-body is a great project. If I use it again, I will solve it.

I just can't help you with that given snippet and without more info.