Node Express——使用多个请求参数会破坏get处理程序发送的HTML文件的CSS样式

Node Express——使用多个请求参数会破坏get处理程序发送的HTML文件的CSS样式,express,parameters,request,Express,Parameters,Request,编辑:我发现了问题——我的样式表是相对链接的,而它本应该是绝对链接的 当有人点击一个生成的链接时,就会调用这个express get处理程序——它工作得很好,但后来我尝试添加第二个请求参数,现在呈现的页面似乎没有任何CSS样式,只是一直在加载 首先,我只使用:creator请求参数。代码如下: //djApp.get('/:creator/:hash', function (req, res) { djApp.get('/:creator/', function (req, res) {

编辑:我发现了问题——我的样式表是相对链接的,而它本应该是绝对链接的

当有人点击一个生成的链接时,就会调用这个express get处理程序——它工作得很好,但后来我尝试添加第二个请求参数,现在呈现的页面似乎没有任何CSS样式,只是一直在加载

首先,我只使用
:creator
请求参数。代码如下:

  //djApp.get('/:creator/:hash', function (req, res) {
  djApp.get('/:creator/', function (req, res) {
    var path = req.params.creator.toLowerCase();
    for(booth in boothList) {
      if (path == boothList[booth].creator.toLowerCase()) {
        res.sendFile(__dirname+'/public/index.html', setTimeout(function () {
          for (c in clients) {
            if (clients[c].url && clients[c].url == path) {
              clients[c].socket.emit('redirectUser', {'booth': boothList[booth]});
            }
          }
        }, 500));
        /*var hash = req.params.hash;
        if (hashes.indexOf(hash) > -1) {
          console.log("path is "+path+"\nhash is "+hash+"\nhashIndex is "+hashes.indexOf(hash));
          hashes.splice(hashes.indexOf(hash), 1);
          res.sendFile(__dirname+'/public/index.html', setTimeout(function () {
            for (c in clients) {
              if (clients[c].url && clients[c].url == path) {
                clients[c].socket.emit('redirectUser', {'booth': boothList[booth]});
              }
            }
          }, 500));
        }*/
      }
    }
  });
});

djApp.use('/', express.static(__dirname+'/public'));
  //djApp.get('/:creator/', function (req, res) {
  djApp.get('/:creator/:hash', function (req, res) {
    var path = req.params.creator.toLowerCase();
    for(booth in boothList) {
      if (path == boothList[booth].creator.toLowerCase()) {
        var hash = req.params.hash;
        if (hashes.indexOf(hash) > -1) {
          console.log("path is "+path+"\nhash is "+hash+"\nhashIndex is "+hashes.indexOf(hash));
          hashes.splice(hashes.indexOf(hash), 1);
          res.sendFile(__dirname+'/public/index.html', setTimeout(function () {
            for (c in clients) {
              if (clients[c].url && clients[c].url == path) {
                clients[c].socket.emit('redirectUser', {'booth': boothList[booth]});
              }
            }
          }, 500));
          /*res.sendFile(__dirname+'/public/index.html', setTimeout(function () {
            for (c in clients) {
              if (clients[c].url && clients[c].url == path) {
                clients[c].socket.emit('redirectUser', {'booth': boothList[booth]});
              }
            }
          }, 500));*/
        }
      }
    }
  });
});

djApp.use('/', express.static(__dirname+'/public'));
这是处理程序正在使用的生成URL:

http://localhost:3001/Aweeeezy
http://localhost:3001/Aweeeezy/a35969699812c71ef8fde58cafebe644dd18ed7b
下面是一个屏幕截图,演示HTML已发送,并按预期正确链接CSS和JS:

接下来,我用单个请求参数和发送html文件的逻辑块注释掉app.get行,然后用两个请求参数和发送html文件的相同逻辑块取消对app.get行的注释,只是它位于与附加请求参数相关的if条件内。代码如下:

  //djApp.get('/:creator/:hash', function (req, res) {
  djApp.get('/:creator/', function (req, res) {
    var path = req.params.creator.toLowerCase();
    for(booth in boothList) {
      if (path == boothList[booth].creator.toLowerCase()) {
        res.sendFile(__dirname+'/public/index.html', setTimeout(function () {
          for (c in clients) {
            if (clients[c].url && clients[c].url == path) {
              clients[c].socket.emit('redirectUser', {'booth': boothList[booth]});
            }
          }
        }, 500));
        /*var hash = req.params.hash;
        if (hashes.indexOf(hash) > -1) {
          console.log("path is "+path+"\nhash is "+hash+"\nhashIndex is "+hashes.indexOf(hash));
          hashes.splice(hashes.indexOf(hash), 1);
          res.sendFile(__dirname+'/public/index.html', setTimeout(function () {
            for (c in clients) {
              if (clients[c].url && clients[c].url == path) {
                clients[c].socket.emit('redirectUser', {'booth': boothList[booth]});
              }
            }
          }, 500));
        }*/
      }
    }
  });
});

djApp.use('/', express.static(__dirname+'/public'));
  //djApp.get('/:creator/', function (req, res) {
  djApp.get('/:creator/:hash', function (req, res) {
    var path = req.params.creator.toLowerCase();
    for(booth in boothList) {
      if (path == boothList[booth].creator.toLowerCase()) {
        var hash = req.params.hash;
        if (hashes.indexOf(hash) > -1) {
          console.log("path is "+path+"\nhash is "+hash+"\nhashIndex is "+hashes.indexOf(hash));
          hashes.splice(hashes.indexOf(hash), 1);
          res.sendFile(__dirname+'/public/index.html', setTimeout(function () {
            for (c in clients) {
              if (clients[c].url && clients[c].url == path) {
                clients[c].socket.emit('redirectUser', {'booth': boothList[booth]});
              }
            }
          }, 500));
          /*res.sendFile(__dirname+'/public/index.html', setTimeout(function () {
            for (c in clients) {
              if (clients[c].url && clients[c].url == path) {
                clients[c].socket.emit('redirectUser', {'booth': boothList[booth]});
              }
            }
          }, 500));*/
        }
      }
    }
  });
});

djApp.use('/', express.static(__dirname+'/public'));
这是处理程序正在使用的生成URL:

http://localhost:3001/Aweeeezy
http://localhost:3001/Aweeeezy/a35969699812c71ef8fde58cafebe644dd18ed7b
输出为:

path is aweeeezy
hash is a35969699812c71ef8fde58cafebe644dd18ed7b
hashIndex is 0
因此,您可以看到,在处理程序中,所有内容都按预期工作--至少在代码与工作案例相同的情况下,但我的浏览器现在呈现的页面如下所示:

真正的区别在于请求处理程序URL中的
/:creator
/:creator/:hash
之间的区别——但我看到的每个使用多个参数的示例都与我试图做的不同


怎么回事

将指向样式表的链接更改为绝对链接。

将指向样式表的链接更改为绝对链接。

找到解决方案了吗。同样的事情发生在我身上?@aweeezyd你能找到解决办法吗。同样的事情发生在我身上?@aweeezy