2016-12-07 83 views
0

我需要通过API代理,我能想到它是利用人体解析器来获得对req.body访问唯一可以做的过程中添加有几件事情来编辑要求的身体。添加bodyParser()挂起所有API请求

const compression = require('compression'); 
const express = require('express'); 
const request = require('request'); 
const cookieParser = require('cookie-parser'); 
const bodyParser = require('body-parser'); 
const app = express(); 

// Add body parser 
app.use(bodyParser.json()); 

// Enable gZip compression 
app.use(compression()); 

// Disable X-Powered-By header for security reasons 
app.disable('x-powered-by'); 

// Add cookie parser 
app.use(cookieParser()); 

// Example proxy 
app.post('/api', (req, res) => { 
    const url = 'https://my.api.com'; 
    req.pipe(request({ 
    url, 
    headers: { 
     'Content-Type': 'application/json', 
     Accept: 'application/json' 
    }, 
    auth: { 
     user: 'user', 
     pass: 'password' 
    } 
    })).pipe(res); 
}); 

但是,一旦我添加身体分析器api挂起,它是悬而未决,然后超时。注意我并没有在本例中添加任何机构操纵然而,当我需要解决这个API第一吊。

回答

1

可以前bodyParser移动压缩中间件和添加一个bodyParser.urlencoded只是bodyParser.json如下之前解析application/x-www-form-urlencoded

// Enable gZip compression 
app.use(compression()); 

// parse application/x-www-form-urlencoded 
app.use(bodyParser.urlencoded({ 
    extended: true 
})); 

// Add body parser 
app.use(bodyParser.json()); 
+0

问题仍然 – Ilja

+0

可以为您注释掉'compression'和检查? – Aruna

+0

还是没有解决问题,问题似乎是从管道的到来,使用像https://github.com/dominictarr/connect-restreamer东西,我会稍微改写它,使用它 – Ilja

相关问题