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

时间:2017-02-18 17:59:30

标签: javascript node.js ejs

我有以下代码。我想打印出该目录中的文件名并附加一个href标签。下面的代码工作,但只有我的node.js应用程序正在监听的端口号仍然存在。我怎么能删除它(所以我的hrefs实际上会工作?)。与截断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>

1 个答案:

答案 0 :(得分:0)

通过阅读您的评论,您的问题似乎不在您认为的位置。 您看到的// address of the notebook var address = "http://localhost:8888/notebooks/Untitled.ipynb"; // auth token from Jupyter console var authToken = "af6bc1d90688bb6c26aeb206b8690e4855d27ef8d265b1bc"; // cell number with a widget output var cellNumber = 10; // this function is used to verify that a page is fully loaded // source: https://github.com/ariya/phantomjs/blob/master/examples/waitfor.js function waitFor(testFx, onReady, timeOutMillis) { var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, start = new Date().getTime(), condition = false, interval = setInterval(function() { if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) { // If not time-out yet and condition not yet fulfilled condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); } else { if(!condition) { // If condition still not fulfilled (timeout but condition is 'false') console.log("'waitFor()' timeout"); phantom.exit(1); } else { // Condition fulfilled (timeout and/or condition is 'true') console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms."); typeof(onReady) === "string" ? eval(onReady) : onReady(); clearInterval(interval); //< Stop this interval } } }, 250); //< repeat check every 250ms }; // log in to a notebook using a token function logIn() { console.log("Logging in"); page.evaluate(function(token) { document.forms[0].password.value = token; document.forms[0].submit(); }, authToken); } // wait for a notebook to fully load, find the // needed output cell and save it as a PNG file function saveAsPNG() { console.log("Saving PNG") // Wait for 'notebook-container' to be visible waitFor(function() { // Check in the page if a specific element is now visible return page.evaluate(function() { return $("#notebook-container").is(":visible"); }); }, function() { console.log("The notebook-container element should be visible now."); var clipRect = page.evaluate(function(cell){ // we are selecting only the output cell var searchStr = 'div.input_prompt:contains("[' + cell + ']:")'; var parentCell = $(searchStr).parents('div.cell')[0]; // get only the data div var outputSubarea = $(parentCell).find('div.output_subarea')[0]; // get the coordinates of the data div return outputSubarea.getBoundingClientRect() }, cellNumber); page.clipRect = { top: clipRect.top, left: clipRect.left, width: clipRect.width, height: clipRect.height }; page.render('example.png'); phantom.exit(); }); } var page = require('webpage').create(); // it seems, viewportSize should fully cover the // the rendered div position, or nothing will be saved. page.viewportSize = { width: 5000, height: 5000 }; page.open(address, function (status) { // Check for page load success if (status !== "success") { console.log("Unable to open a page"); } else { // Wait for 'password_input' to be visible waitFor(function() { // Check in the page if a specific element is now visible return page.evaluate(function() { return $("#password_input").is(":visible"); }); }, function() { console.log("The password_input element should be visible now."); logIn(); saveAsPNG(); }); } }); 会自动写入,因为您指定了一个相对于当前网址的href:

如果当前网址为phantomjs script.js,转到相对网址:8080实际上会将您重定向到localhost:8080/Week4/index.html

如果这不是您打算工作的方式,则必须指定绝对URL。例如,使用itemlist会将您重定向到另一台服务器(或同一服务器上的另一个侦听器)。

<小时/> 我不知道你想要实现什么,但对我而言,你期望的行为听起来并不是很好。链接不应重定向到同一服务器上的其他端口,除非您正在执行非常具体的操作。

如果您能告诉我们真正想要实现的目标,我们可以为您提供更好的帮助。