小编Tra*_*211的帖子

无法在本地主机中将查询参数与expressJs api一起使用

在我的index.js档案中

app.use('/api/v1/users', userRouter)
Run Code Online (Sandbox Code Playgroud)

在路由器文件中

router.get("/:id", getUserDataById);
Run Code Online (Sandbox Code Playgroud)

在邮递员中:

我的 GET URL 如下所示:http://localhost:3000/api/v1/users?id=120622

它给出的错误:

Cannot GET /api/v1/users
Run Code Online (Sandbox Code Playgroud)

我认为,这就是根据我遵循的文档和教程给出查询参数的方式,但这个错误不会消失。

如果我删除查询,那么其他端点可以完美工作。

我无法发现这里出了什么问题。

过去两天我一直坚持这个问题。只需一个解决此问题的提示,就会对我有很大帮助。

提前致谢!

javascript node.js express postman

5
推荐指数
1
解决办法
360
查看次数

有没有办法使用单个循环比较数组中的每个元素?

我需要打印一个int数组的任何两个元素之间的最小差异。

数组的每个元素A小于等于其长度。

1 <= A[i] <= A.length;
Run Code Online (Sandbox Code Playgroud)

我已经在Java中尝试了以下给定的方法-但是,当数组大小为〜10 ^ 5时,这需要1秒钟以上的时间才能找到结果。

我认为这可能是一种幼稚的方法。有什么办法可以进一步优化它?能做到O(n)时间复杂吗?

static int findResult(int[] arr)
        {
                 int max =  Integer.MAX_VALUE;
                 HashSet<Integer> hs = new HashSet<Integer>();
                 for(int obj : arr)
                 {
                     hs.add(obj);
                 }
                if(hs.size()==1 )
                {
                    return 0;             // if all elements are same
                }
                 for(int i=0; i<arr.length; i++)
                 {  
                     for(int j=i+1; j<arr.length; j++)
                     {
                         int value = Math.abs(a[i]-a[j]);
                         if(value<max)
                         {
                            max = value;
                         }

                      }         
                }
        return max;  // returns the smallest positive difference
    }
Run Code Online (Sandbox Code Playgroud)

algorithm optimization array-algorithms

3
推荐指数
1
解决办法
379
查看次数