我正在尝试使用请求模块将数据发送到后端(快速),但是快速后处理程序正在打印正文未定义。并且快递正在发送404(错误请求)。 (注意:-我已经激活了跨域资源共享)
我的React代码:-
const submitHandler = (e) =>{
e.preventDefault();
//const url = 'http://localhost:3001/user/login';
const response = new Promise((resolve,reject)=>{
let options = {
email: 'me@example.com',
password: 'myPassword'
};
const data = JSON.stringify(options);
submit === true ?
request.post({url:'http://localhost:3001/user/login',headers:{'content-
type' : 'application/x-www-form-urlencoded'},body:data},(error,res)=>{
resolve(res);
}) :
reject('something went wrong!')
});
response.then((res)=>{
console.log(res)
}).catch((e)=>{
console.log(e);
})
}
我的Express代码:-
app.post('/user/login',(req,res)=>{
console.log(req)
try{
const data = JSON.parse(req)
console.log('POST request initiated !',data.body);
console.log(data);
res.send('Post request got it! ',+data);
}catch(e){
res.status(400).send('Bad Request !');
}
})
app.listen(port,()=>{
console.log('Server is running at PORT ',port)
})
输出为:-
正文:{},
路线:
路线{
路径:“ / user / login”,
堆栈:[[Layer]],
方法:{post:true}}
答案 0 :(得分:0)
您可以在Express中使用内置的json中间件,这样就不必单独解析每个请求:
df.loc[:,'direction'] = np.NaN
df.loc[df.index == 0, "direction"] = "WEST"
# mask is for finding when a signal hasnt changed in three seconds, but now has
mask = (df.trajectory != df.trajectory.shift(1)) & (df.trajectory == df.trajectory.shift(-1)) & (df.trajectory == df.trajectory.shift(-2))
df.loc[(mask) & (df['trajectory'] == 'LEFT') & (df['direction'].dropna().shift() == "WEST"),'direction'] = 'SOUTH'
df.loc[(mask) & (df['trajectory'] == 'LEFT') & (df['direction'].dropna().shift() == "SOUTH"),'direction'] = 'EAST'
df.loc[(mask) & (df['trajectory'] == 'LEFT') & (df['direction'].dropna().shift() == "EAST"),'direction'] = 'NORTH'
df.loc[(mask) & (df['trajectory'] == 'LEFT') & (df['direction'].dropna().shift() == "NORTH"),'direction'] = 'WEST'
df.loc[(mask) & (df['trajectory'] == 'RIGHT') & (df['direction'].dropna().shift() == "WEST"),'direction'] = 'NORTH'
df.loc[(mask) & (df['trajectory'] == 'RIGHT') & (df['direction'].dropna().shift() == "SOUTH"),'direction'] = 'WEST'
df.loc[(mask) & (df['trajectory'] == 'RIGHT') & (df['direction'].dropna().shift() == "EAST"),'direction'] = 'SOUTH'
df.loc[(mask) & (df['trajectory'] == 'RIGHT') & (df['direction'].dropna().shift() == "NORTH"),'direction'] = 'EAST'
df.loc[:,'direction'] = df.direction.fillna(method="ffill")
print(df[['trajectory','direction']])
使用它后,您可以在request.body属性中访问帖子数据。
顺便说一句,由于尝试解析不是json格式的整个请求对象,因此出现了错误。