简体   繁体   English

Node.js:一次重定向并发送页面

[英]Node.js: Redirect and send page at once

I'm trying to build simple server for HTML pages in Node.js. 我正在尝试为Node.js中的HTML页面构建简单的服务器。 My problem is, when I navigate to (for example) http://localhost:8080/someDirectory , browser thinks that someDirectory is file (in reality, it is interally redirected to someDirectory/index.html ). 我的问题是,当我导航到(例如) http://localhost:8080/someDirectory ,浏览器认为someDirectory是文件(实际上,它在内部被重定向到someDirectory/index.html )。

So when I do <script src="./script.js"></script> , it tries to use http://localhost:8080/script.js , instead of http://localhost:8080/someDirectory/script.js . 因此,当我执行<script src="./script.js"></script> ,它将尝试使用http://localhost:8080/script.js而不是http://localhost:8080/someDirectory/script.js

Only solution I can think of is to redirect browser to /someDirectory/ (with slash at the end). 我能想到的唯一解决方案是将浏览器重定向到/someDirectory/ (末尾带有斜杠)。 Then, the browser correctly understands that it is directory and not file. 然后,浏览器正确地知道它是目录而不是文件。

My problem is, I don't want to make my server slow, because of double request (client gets redirected, sends another request, and then finally gets the webpage). 我的问题是,由于双重请求,我不想让我的服务器变慢(客户端被重定向,发送另一个请求,然后最终获得网页)。 Is there any posibility to send send page and just notify the browser of change, instead of using 302 header to redirect? 是否有发送发送页面并仅通知浏览器更改的可能性,而不是使用302标头重定向?


EDIT: When I visit some page running on Apache server, when I enter http://localhost/someDirectory , the trailing slash get automatically appended. 编辑:当我访问在Apache服务器上运行的某些页面时,当我输入http://localhost/someDirectory ,尾部的斜杠会自动添加。 I'm just curious how it is done (no additional PHP or javascript). 我只是很好奇它的完成方式(没有其他PHP或javascript)。

No, it's not possible, and wouldn't even be the right solution. 不,这是不可能的,甚至不是正确的解决方案。 You can certainly send content with a redirect, but it's pointless because the browser won't display it... it will immediately follow the redirect. 您当然可以通过重定向发送内容,但这毫无意义,因为浏览器不会显示它……它会立即跟随重定向。

If you want /someDirectory/ , that is different than /someDirectory . 如果要/someDirectory/ ,则与/someDirectory不同。 You must redirect. 您必须重定向。

I recommend using history.replaceState ( MDN ). 我建议使用history.replaceStateMDN )。 This will work in most modern browsers (most users) but not in older browsers. 这将适用于大多数现代浏览器(大多数用户),但不适用于较旧的浏览器。 Since older browsers are dwindling, it should become less of a problem as time moves on. 由于较旧的浏览器正在逐渐减少,因此随着时间的流逝,它应该不再是一个问题。 This should do the trick - you will need to paste it directly in your HTML markup for this to work: 这应该可以解决问题-您需要将其直接粘贴到HTML标记中才能正常工作:

if ( !window.location.pathname.match(/\/$/) ) {
    if ( window.history && window.history.replaceState ) {
        window.history.replaceState(null, null, window.location.pathname + '/');
        document.write('<script src="./script.js"></script>');
    } else
        window.location.replace( window.location.pathname + '/');
} else {
    document.write('<script src="./script.js"></script>');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM