问题
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".
- First setup the contact route in your server code:
//Setting up the contact route app.get("/contact", function (req, res) {
res.render("contact", {}); });
- 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