How to define complex routes with named parameters in express.js?

后端 未结 2 1429
再見小時候
再見小時候 2021-02-02 16:31

I want to set up some more complex routes that can include a number of optional parameters. Preferably, I would like to use named parameters for ease of access. Here is an examp

2条回答
  •  死守一世寂寞
    2021-02-02 17:33

    you can send data to view as follow:

    //in the server side ...
     app.get('/search/:qkey/:qvalue', function(req, res){
        res.write(JSON.stringify({
          qkey:req.params.qkey;
          qvalue:req.params.qvalue;
        }));
     });
    

    and in the client side... call to ajax

    $.ajax({
      type:"POST",
      url:"/search/"+qkey+"/"+qvalue,
      success: function(data){
        var string = eval("(" + data + ")");
        //you access to server response with
        console.log(string.qkey+" and "+ string.qvalue);
      }
    });    
    

提交回复
热议问题