小编hav*_*ish的帖子

对于KOA中的POST请求,请求正文未定义

我是nodejs的初学者.我正在尝试创建一个使用koa框架来处理HTTP请求的HTTP服务器.以下是我的服务器的代码.

app.use(function * (next){
    var ctx = this;

    var resource = url.parse(ctx.request.url, true, true).pathname;
    switch(resource) {
            case '/':
                    resource = config.app.root + '/dist/index.html';
                    ctx.type = mime.lookup(resource);
                    ctx.body = fs.readFileSync(resource, 'utf-8');
                    ctx.response.set('Access-Control-Allow-Origin', '*');
                    break;
            case '/fudge/contact':
                    console.log('============================');
                    console.log(ctx.request); // no body
                    console.log('============================');
                    console.log(ctx.req);     // no body
                    console.log('============================');
                    console.log(ctx.request.body) // is undefined
                    break;
            default:
                    resource = config.app.root + resource;
                    ctx.type = mime.lookup(resource);
                    ctx.body = fs.readFileSync(resource, 'utf-8');
                    ctx.response.set('Access-Control-Allow-Origin', '*');
                    break;
    }});
Run Code Online (Sandbox Code Playgroud)

如'/ fudge/contact'中所述,ctx.request.body未定义.但是,当我检查ctx.request或ctx.req时,它显示内容长度为98(或非零值).

以下是我得到的输出:

============================

{ 
method: 'POST',
  url: '/fudge/contact',
  header: 
   { host: …
Run Code Online (Sandbox Code Playgroud)

javascript http node.js koa

1
推荐指数
1
解决办法
5491
查看次数

标签 统计

http ×1

javascript ×1

koa ×1

node.js ×1