Can I link to a .ejs-page from a .html-page?

寵の児 提交于 2019-12-31 05:10:15

问题


I am making a webpage and I have been looking on stackoverflow on how to link to .ejs-files from a .html-file.

People are saying the following

In index.html:

<li><a href="/twitter">Twitter</a></li>

In script.js:

app.get('/twitter',function(req,res){
 res.render('twitter', { });
});

But it does not work for me. It says "Your file was not found". It works on localhost, but not when I first click on index.html and then click on twitter.ejs page from there. My code looks like this: jsfiddle.

twitter.ejs is in a view folders, while index is outside of this folder.

What is wrong?


回答1:


The situation is you're trying to link directly to a view template.

You need to create a route to a twitter controller that then calls your template. It would look something like this:

router.get('/twitter', twitterController.twitter_view);




回答2:


If your .ejs file is called "contact.ejs" as an example, then the route you're targeting may be something like "/contact".

  1. First setup the contact route in your server code:

//Setting up the contact route app.get("/contact", function (req, res) {

res.render("contact", {}); });

  1. Update the href in the corresponding anchor tag to: href="/contact"

I am using expressJS and EJS for this example. So these must be setup first.



来源:https://stackoverflow.com/questions/46939535/can-i-link-to-a-ejs-page-from-a-html-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!