Html Express res.sendFile()和查询字符串

Html Express res.sendFile()和查询字符串,html,node.js,express,static,Html,Node.js,Express,Static,我已经成功地使用res.sendFile()实现了为静态文件提供服务,但是如果我添加一些querystring,它就不起作用了 例如,下面的代码工作得非常好 res.sendFile(path.join(__dirname, '../public', '/index.html')); 但如果我这样做,它就失败了 res.sendFile(path.join(__dirname, '../public', '/index.html?id=' + req.params.id)); res.sen

我已经成功地使用res.sendFile()实现了为静态文件提供服务,但是如果我添加一些querystring,它就不起作用了

例如,下面的代码工作得非常好

res.sendFile(path.join(__dirname, '../public', '/index.html'));
但如果我这样做,它就失败了

res.sendFile(path.join(__dirname, '../public', '/index.html?id=' + req.params.id));

res.sendFile(path.join(__dirname, '../public', '/index.html?id=123'));
然后我得到下面的错误

ENOENT, stat '/Users/krishnandu/Documents/Project/public/index.html?id=123'

404

Error: ENOENT, stat '/Users/krishnandu/Documents/Project/public/index.html?id=123'
    at Error (native)

不能使用
res.sendFile()
传递查询字符串参数。必须将文件路径指定为
res.sendFile()

语法是:

res.sendFile(path [, options] [, fn])
所以你能做的是

  • 将查询字符串与路由一起使用,例如route1(请参阅下面的代码)

  • 在路由1的GET方法中,使用res.sendFile()


  • 另请参见和。

    此答案是为了进一步补充@Marie Sajan的答案,在评论中涵盖@Sky提出的问题。

    要在使用@Marie Sajan的答案后访问
    index.html中的
    id
    ,您可以使用普通的客户端javascript。您可以在html文件的
    标记中执行以下操作:

    var query=location.href.split(“?”[1]

    这将得到一个字符串,如“id=123&name=ted”。 从这里可以使用javascript获取
    id
    的值。完整代码可能如下所示:

    var query = location.href.split("?")[1]; //A string of all parameters in the URL
    var params = query.split("&"); //Turns the string into an array of parameters
    var id; //To store the value of id
    
    params.forEach((param, index) => {
        //This checks each key-value pair, in the format key=value, for the specific id key we want
        var key = param.split("=")[0]; //In id=123, this would be "id"
        var value = param.split("=")[1]; //In id=123, this would be "123" (as a String)
        if (key == "id") id = value;
    });
    
    现在javascript变量
    id
    将包含值“123”

    如果除了
    id
    键之外,您还需要查找更多查询参数的值,那么只需在
    forEach
    中添加更多
    If
    语句即可检查这些特定键

    在诸如“”之类的链接中,当作为客户端javascript直接在
    标记中的HTML文件中实现时,或者在HTML文件使用的单独客户端js文件中实现时,此代码将能够获得
    id
    的值“123”。请注意,@Marie Sajan的答案完全是服务器端代码,而此代码将用于客户端


    这个答案没有回答原来的问题,它只会根据本页浏览者的需要,为问题的公认答案添加更多有用的内容。

    我认为您应该改用
    res.render
    ,并将参数传递给视图template@CodeBeginner我正在呈现一个静态HTML文件,而不是基于jade/ejs的模板文件。我如何访问
    id
    index.html
    中,除了执行
    var query=location.href.split(“?”[1]
    ,您还可以执行
    var query=location.search
    。它具有通用的主要浏览器支持。
    var query = location.href.split("?")[1]; //A string of all parameters in the URL
    var params = query.split("&"); //Turns the string into an array of parameters
    var id; //To store the value of id
    
    params.forEach((param, index) => {
        //This checks each key-value pair, in the format key=value, for the specific id key we want
        var key = param.split("=")[0]; //In id=123, this would be "id"
        var value = param.split("=")[1]; //In id=123, this would be "123" (as a String)
        if (key == "id") id = value;
    });