Javascript Hapijs查询参数未定义

Javascript Hapijs查询参数未定义,javascript,node.js,https,hapijs,query-parameters,Javascript,Node.js,Https,Hapijs,Query Parameters,我目前正在与hapi合作,我遇到了一个问题,我似乎找不到任何解决方案或之前提到的问题。当我发送以下请求时,只有我的第一个查询参数在request.query对象中 curl -H "Content-Type: application/json" -X PUT https://localhost:3000/lists/{username}/{listname}?token='token'&resource_id='resource_id' 将{}和'中的项目替换为实际名称 我的路线目前是

我目前正在与hapi合作,我遇到了一个问题,我似乎找不到任何解决方案或之前提到的问题。当我发送以下请求时,只有我的第一个查询参数在request.query对象中

curl -H "Content-Type: application/json" -X PUT https://localhost:3000/lists/{username}/{listname}?token='token'&resource_id='resource_id'
{}
'
中的项目替换为实际名称

我的路线目前是这样写的

server.route({
    method: 'PUT',
    path: '/lists/{username}/{listname}',
    handler: function(request, reply) {
        const username = encodeURIComponent(request.params.username);
        const listname = encodeURIComponent(request.params.listname);
        const resource_id = request.query.resource_id;
        const token = request.query.token;
        console.log(request.query);
        verify(token, username, {callback}, listname, resource_id, reply);
    }
});
console.log
调用会导致

{ token: 'token' }

如果我执行
console.log(resource\u id)
操作,我会在控制台中得到“undefined”。hapi的文档说明所有查询参数都应该在
request.query
对象中找到。出于某种原因,这并没有发生。我已经阅读了hapijs文档,查看了我的API调用,并且阅读了人们处理查询参数的示例。知道这里发生了什么吗?

问题在于curl命令而不是HapiJS

试着跑步
curl-H“内容类型:application/json”-X PUT”https://localhost:3000/lists/{username}/{listname}?token=token&resource\u id=resource\u id“


它是被解释为命令结尾的查询字符串中的符号。有关命令不起作用的原因的解释,请参见

问题在于触发curl请求。curl请求中有
&
,URL不在引号中;因此,
&
变成了一个shell,修改后可以启动一个分离的进程。您的路由工作正常,只需使用带引号的URL启动curl


curl-H“内容类型:application/json”-X PUT”https://localhost:3000/lists/{username}/{listname}?token='token'&resource\u id='resource\u id'

如果您想从查询参数获取数据,那么您应该执行以下代码:

server.route({
    method: 'PUT',
    path: '/lists/{username}/{listname}',

    validate: {
        params: {
            username: Joi.string().min(1).max(15).required(),
            listname:Joi.string().min(1).max(15).required(),

        },
   query: {
            token:    Joi.number().integer().min(1).max(100).required(),
            resource_id: Joi.number().integer().min(1).max(100).required(),
         }
       },
    handler: function (request, reply) {
         const username = encodeURIComponent(request.params.username);
         const listname = encodeURIComponent(request.params.listname);
         const resource_id = request.query.resource_id;
         const token = request.query.token;
         console.log(request.query);
         verify(token, username, {callback}, listname, resource_id, reply);
         reply("Hello There..");
    }

您可以使用上述代码获取参数/查询数据。

谢谢!我忘了shell会看到“&”并在后台运行该过程,这让我很吃惊。