如何快递路线类似的网址链接?

时间:2017-07-23 13:37:56

标签: node.js express

使用node.js开发Web应用程序并表达。

我有两个网址要区分:

  1. / API / V1 /源?ID = 122323
  2. / API / V1 /源时间戳= 1555050505&安培;计数= 10
  3. 我提出了一个天真的解决方案。我将这些类似的URL留给一个路由方法,并使用if eles指定解决方案,即:

    if(id){
    //solution with id 
    }
    
    if(timestamp&&count){
    //solution with timestamp and count but without id 
    }
    

    显然,这不干净。因为在将来,我可能想要添加新的字段,这会使这个路由器变得庞大而丑陋。

    那我怎么能克服这个呢?或者改变url结构。我想建立一个Restful api。

2 个答案:

答案 0 :(得分:2)

尝试将列表中的所有属性放在一起,并使用Array#every检查Array中的所有值是否计算为true。

也许是这样的:

(( /* req, res */)=>{
  // Dummy express Request Object
  const req = {
    params : {
      //id : '123',
      count : 10,
      timestamp : 1555050505,
      newParameter : 'whatever value'
    }
  }

  let { params } = req;

  let {
      id
    , count
    , timestamp
    , newParameter
  } = params;


  if(id){
    console.log('Action with id');
    return;
  }

  let secondConditionArray = [
    count, timestamp, newParameter
  ];


  if( secondConditionArray.every(Boolean) ){
    console.log('Second Action')
  } else {
    console.log('Some values are no truthy')
  }
})()

答案 1 :(得分:0)

您可以使用using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ballmove : MonoBehaviour { private Rigidbody2D rb; public float speed; public GameObject top; private Vector2 direct; public int p2; public int p1; public Text scoretext; public Text scoretext2; void Start() { rb = GetComponent<Rigidbody2D>(); speed = 3f; InvokeRepeating("speedUp", 10f, 10f); p1 = 0; p2 = 0; } void Update() { if (rb.velocity == new Vector2(0f, 0f)) { if (Input.GetKeyDown(KeyCode.Space)) { rb.velocity = new Vector2(2f, 1f) * speed; } } } void OnTriggerEnter2D(Collider2D other) { if (other.gameObject.tag == "finish2") { Debug.Log("This if is working"); p2++; Instantiate(top, new Vector2(-6.4f, -0.2f), Quaternion.identity); Destroy(this.gameObject); updateScore2(); } if (other.gameObject.tag == "finish") { p1++; Instantiate(top, new Vector2(6.32f, -0.2f), Quaternion.identity); Destroy(this.gameObject); updateScore(); } } void updateScore() { scoretext.text = p1.ToString(); } void updateScore2() { scoretext2.text = p2.ToString(); } void speedUp() { direct = rb.velocity.normalized; rb.velocity += direct * 2f; } }

获取Url参数
req.params