What is the proper way to link a .ejs file in HTML using the href attribute

I've been working on constructing a login system using Nodejs and express. The login page has all the necessary forms built with ejs.

However, in my index.html file, there's a button that is supposed to redirect users to the login page:

<button id="loginBtn">
<a href="views/login.ejs"><LI> LOGIN </LI></a>
</button>

The issue I'm facing is that instead of displaying the actual page, it prints out the ejs code for the login page. Can anyone point out what mistake I might be making here?

Answer №1

When rendering a view in expressjs, all you need to do is specify an endpoint and call that specific endpoint.

It's important not to directly call the location of the .ejs file.

For instance, in the following code snippet, login.ejs will be rendered automatically behind the scenes when res.render('login') is called:

 router.get('/home', function(req, res, next) {
    res.render('login');
 });

Your hyperlink should only call the endpoint mapped by the router:

 <a href="/home"><li> LOGIN </li></a>

If you are working on UI design using EJS and only responsible for the front-end part, you can create dummy data to render an EJS page without running the entire website with a database.

For example, assuming there is dummy data available for rendering an EJS page, you can achieve this by obtaining the data from the developer and writing code like the following:

 router.get('/home', function(req, res, next) {
    var data = {"name": "Emmanuel"}
    res.render('login', data);
 });

In this code snippet, the data is hardcoded as temporary.

Answer №2

Although this question is quite old, I stumbled upon the OP's post while seeking a solution to a similar issue of my own.

  // In order to navigate to the 'new post' page, which is generated solely from an ejs template (without HTML), and corresponds to the 'new' endpoint on my express router object
  <a href="new" class="new-post-link">new post</a>

  // By clicking this link, the user will be directed to the /new endpoint and display the 'new post' page upon clicking  
  <a href="new" class="new-post-link">new post</a>

  // Addressing the specific problem mentioned by the OP:

  // original
  <button id="loginBtn">
    <a href="views/login.ejs"><LI> LOGIN </LI></a>
  </button>
  
  // solution
  <button id="loginBtn">
    <a href="login"><LI> LOGIN </LI></a>
  </button>

I trust that this explanation proves beneficial to someone in need.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to choose a particular Excel spreadsheet in Angular before importing the file?

I am currently working on a new feature that allows users to choose a specific Excel worksheet from a dropdown list before importing a file. However, I am facing some difficulty in adding the worksheet names to the dropdown list. Right now, I have placehol ...

Implementing the Disabled Attribute on a Dynamically Generated Button Using React

My QWERTY keyboard is dynamically rendered through a component as Bootstrap buttons using bootstrap-react. Each button does not have an ID to avoid relying on IDs in React. When a letter button is clicked, it triggers an onClick event passed as props back ...

Steps to generate binaries for all platforms from NPM modules

Can you explain the process of transforming NODE MODULES into binaries so that they are compatible with all platforms? Windows x86 Windows X64 Linux OS X Additionally, how can we incorporate these binaries into a Node application rather than relying on ...

Can a single div have three different border-bottom styles?

Can three border-bottom styles be applied to a single div element? This is the desired appearance: ...

What could be causing the error I'm encountering when attempting to save a mongoose model?

I'm currently working on developing a Node.JS controller for resetting user passwords. The process involves fetching the user from the database using a reset password token, performing validations, updating the relevant fields, and saving the changes ...

Exploring the depths of npm in the realm of frontend development

Currently, I am delving into the realm of Javascript/Node development through self-teaching. While I grasp the concept of npm handling package installations for my Node application on the server side, I am struggling to comprehend how npm assists me with ...

I encounter a CORS issue on Heroku, but strangely it only occurs after 20 minutes of deploying. Prior to that time frame, everything functions seamlessly

Today, I encountered a strange issue while hosting my first Node JS backend on Heroku. Everything runs smoothly when I register/login immediately after deploying the backend. However, if I try to register/login after about 15 minutes, I start receiving a C ...

Transform Typescript into compiled css files without using any additional tools

Currently, I am working on a monorepo project where the main project relies on another project called components. When running the entire monorepo, the main project utilizes webpack.dev while the components project simply uses the TypeScript compiler. It l ...

Ways to resolve the angular error "Encountering an unhandled exception: Unable to locate module 'typescript' "?

I'm encountering errors when running ng serve. I've attempted the following code as well, but I'm still facing the same error: npm install -g typescript https://i.sstatic.net/Gyi71.png Error displayed in text format D:\xampp\htd ...

Can multiple `npm install` commands run at the same time?

Is it possible to have concurrent runs of npm install, or will they conflict on shared resources and create potential race conditions? Analyzing the code might not provide a clear answer, but for those working on developing npm, this is likely an implicit ...

Ensure that the image inside a resizable container maintains the correct aspect ratio

I often encounter a situation where I need to showcase a series of thumbnails/images that adhere to a specific aspect ratio within a flexible container. My current approach involves using a transparent, relatively positioned image to enforce the correct as ...

Update D3 data, calculate the quantity of rows in an HTML table, and add animations to SVGs in the final

Attempting to update data in an HTML table using D3 has proven to be quite challenging for me. My task involves changing the data in multiple columns, adjusting the number of rows, and animating SVG elements in each row based on new data arrays. Despite tr ...

Revival through asynchronous functions in Node.js programming

I'm currently working on a nodejs function to remotely delete files via sftp. Here's what my function looks like: function deleteRemoteDirectory(path){ sftp.readdir(path, async(err, files)=>{ if(err) conso ...

Enhance your user interface with Boostrap select icon spacing

Hello, I am currently experiencing an issue with my code where there is a space between the icon on the left and the field. I need to remove this space, does anyone have any ideas on how to do this? Thank you! <div class="col-auto"> <label c ...

Strategies for effectively utilizing the toggle() function within the child class's complexity

I am currently working on implementing a read more toggle for a dynamically generated paragraph containing lengthy text. I am struggling with directing my toggle to specific sections within my code due to the complexity of the structure. Please refer to t ...

The limit for writing requests to the Sheets API per minute in Node.js has been reached

I have encountered quota problems while using the Sheets API from Google with node.js. Despite implementing batch requests and getting my write quota increased to 900 per minute upon request, I still encounter this error (see question below after explanati ...

Issue with sticky-navigation not functioning properly in conjunction with Bootstrap navbar

I'm struggling with getting the sticky-top function to work properly. To work around the issue, I ended up writing some messy javascript code, but I'm hoping someone might have a better idea. My goal is to have the element stick to the top of the ...

Understanding array manipulation in a Jade file using Node.js

I have an array in my view route that is set as follows: [ 'dgdgd', 'gdgdfg', 'gdgd', 'dgdg', 'gdfdg', 'gdg'] In the jade file, I have written the following code: select#e1.col-md-12. ...

Bookshelf.js - Extracting a specific column from a joined table

I am currently working on setting up a many-to-many relationship between my User model and Course model. Users can join multiple courses, and courses can have multiple users (students). To add a unique twist, I am adding a monthly quota of questions that u ...

Design a layer that rests on top of a specific table column

I am currently working with a table that consists of 5 columns (city, 1st quarter, 2nd quarter, 3rd quarter, 4th quarter). My goal is to create an overlay on each column if we have already passed a specific quarter. However, I am encountering difficulties ...