节点管道JSON特定对象/秒到POST请求"/>
表达/节点管道JSON特定对象/秒到POST请求
我建立一个中间件服务。当被调用时,它提供了某些信息的JSON对象。我要抓住特定对象,并传递通过一个新的POST请求。不过,我努力抓住它,因为它的范围之外,我想我只是感到困惑。下面的实施例;
var createStory = {
url: url,
headers: {
'Content-Type': 'application/json',
'Token': Token
},
body: {
current_state: req.body.data.item.user.name,
name: 'API testing posts',
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nullam et dui a nisi molestie vestibulum non id ante. Sed aliquet neque augue, a vestibulum lectus euismod et. Maecenas porta justo quis maximus tempor. Sed ante libero, posuere vitae efficitur sit amet, feugiat at dolor.',
story_type: 'bug',
label_ids: [20949434]
},
json: true
};
app.post('/api/something', function (req, res) {
if (req.body.data.item.conversation_parts.conversation_parts[0].body === 'match'){
request.post(createStory, function (error, response, body) {
console.log('error:', error);
console.log('statusCode:', response && response.statusCode);
console.log('body:', body);
});
res.sendStatus(200);
};
res.sendStatus(200);
});
current_state: req.body.data.item.user.name,
是什么,我想在这里使用 - 我试图从JSON对象我收到拉的用户名和转发到current state
键值对。
该if
语句的工作,因为它的范围之内。
我希望这是有道理的?
回答如下:- 你不是在你的问题在哪里以及如何你的“createStory”变量用来和不客气很清楚。你想使用用户凭据您req.body跨您的应用程序,或只是在这个路由器你提到的访问?你声称“createStory”是一个中间件,但它不是,它只是其他地方挂一个变量。
- 您不能使用路由器功能的“请求”之外。
如果我(觉得)我明白你的目标(这又是不明确的),你有什么是3个选项。要么你附上您的凭据,以您的应用随处访问(记住不确定的地方不适用),如下所示:
app.post('*',(req,res,next)=>{
req.service={'foo':req.body}
req.body.data.item.user.name=req.user.name
return next()
})
现在,它是在所有的路由器,在任何一台路由器只是ADRESS req.service或req.user.name可用。请与passportjs心中可能发生的冲突(如果使用的话),因为它使用req.user变量。
要么
您可以手动添加一个中间件的功能与此参数你想,像这样的任何路由器:
app.post('/api/something', middleware, (req,res)=>{
//middleware is executed, do your job
})
function middleware(req,res,next){
if (req.body.data.item.user.name){
return next();
} else {res.send('no user in request')}
}
要么
只要把你的“createStory”你的路由器这样的内
app.post('/api/something', function (req, res) {
var createStory = {'foo':{'bar':{'data':req.body.data}}}
if(createStory.foo.bar.data){
//do your job}
})
希望能帮助到你。
更多推荐
表达/节点管道JSON特定对象/秒到POST请求
发布评论