简体   繁体   English

Node.js EJS模板:如何从href中删除端口号?

[英]Node.js EJS template: how can I remove the port number from the href?

I have the below code. 我有下面的代码。 I would like to print out the file names in that directory and append an href tag. 我想打印出该目录中的文件名并附加href标记。 The below code works but only the port number my node.js app is listening to is still there. 下面的代码可以工作,但是只有node.js应用正在侦听的端口号仍然存在。 How could I remove that (so my hrefs will actually work?). 我该如何删除它(以便我的href实际起作用?)。 Something with the document object, as in truncating the URL? 与文档对象有关的东西,例如截断URL?

<h1><%= title %></h1>
<p>Welcome to
    <%=t itle %>
</p>
<ul>
    <% for(var i=0; i<fs.readdirSync( './Week4/').length; i++) {%>
        <li>
            <a href="<%= fs.readdirSync('./Week4/')[i] %>">
                <%=f s.readdirSync( './Week4/')[i] %>
            </a>
        </li>
        <% } %>
</ul>

From reading your comments, it seems that your problem is not where you think it is. 通过阅读您的评论,看来您的问题不在您想像的地方。 The :8080 you see is written automatically because you specify an href that is relative to the current URL: 您看到的:8080是自动编写的,因为您指定了相对于当前URL的href:

If the current URL is localhost:8080/Week4/index.html , going to the relative URL itemlist will in fact redirect you to localhost:8080/Week4/itemlist . 如果当前URL是localhost:8080/Week4/index.html ,则转到相对URL itemlist实际上会将您重定向到localhost:8080/Week4/itemlist

If this is not the way you intend things to work, you have to specify an absolute URL. 如果这不是您想要的工作方式,则必须指定一个绝对URL。 For instance, using //localhost:80/Week4/itemlist will redirect you to another server (or another listener on the same server). 例如,使用//localhost:80/Week4/itemlist会将您重定向到另一台服务器(或同一服务器上的另一个侦听器)。


I don't exactly know what you are trying to achieve but, to me, the behavior you are expecting doesn't sound really good. 我不确定您要达到的目标,但是对我来说,您所期望的行为听起来并不好。 A link should not redirect to a different port on the same server except if you are doing something very specific. 链接不应重定向到同一服务器上的其他端口,除非您要进行非常特定的操作。

If you could tell us what you are really trying to achieve, we could help you better. 如果您能告诉我们您真正想要实现的目标,我们可以为您提供更好的帮助。

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

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