node,app.get not found,using parametr

时间:2017-03-31 04:55:53

标签: node.js reactjs express url routing

我想尝试这条路线:GET / exercisesadd?id = google-oauth2%7C10385839303782

FrontEnd:

getResults: function(){
  console.log(this.state.profile.user_id)
  return axios.get('/exercisesadd', {
    params: {
      id: this.state.profile.user_id
    }
  })
  .then(function(response){      
    return response;
  });
}

服务器有:

app.get('/exercisesadd/:id', (req, res) => {
  console.log(req.params)
});

1 个答案:

答案 0 :(得分:0)

在您的情况下,您正在进行查询请求,但您的服务器没有查询请求的路由。

当您在服务器中进行GET /exercisesadd?id=google-oauth2%7C10385839303782时,您必须拥有/exercisesadd?id的路线,您只能拥有参数的路径

试试这个并查看控制台

app.get('/exercisesadd/:id?', (req, res) => {
    console.log('Query', req.query, 'Params', req.params)
  res.send(req.params);
});

获取localhost:3000/exercisesadd?id=google-oauth2%7C10385839303782 req.query将为您提供{ id: 'google-oauth2|10385839303782' }

获取localhost:3000/exercisesadd/13134124252 req.params将为您提供{ id: '13134124252' }