Is there a way to create an internal link to another HTML templating engine page within Express.js?

I am currently facing an issue with two Pug files, index.pug and search.pug, stored in a /views folder. In my index.pug file, I have the following line of code:

    a(href="/search.pug") Search

In my JavaScript file, I have specified the view engine as Pug and set up static file serving for the views folder like this:

app.set('view engine', 'pug');
app.use(express.static('views'));

However, when I click on the link to the search.pug page from the index.pug page, instead of redirecting me to it, the search.pug file gets downloaded. How can I resolve this issue?

I tried changing app.use(express.static('/views')); but now, instead of downloading the page, I receive an error message saying: Cannot GET /search.pug

Answer №1

There is a better way to approach this issue. To properly handle the request in Express, you should follow these steps:

a(href='//yourDomain/search')

app.get("/search", (req, res) => {
    res.render("search.pug");
};

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

State management through Functional Reactive Programming

Many believe that FRP involves managing event streams without the need to explicitly handle state. An example of this can be found here: Others argue in favor of FRP by highlighting the challenges associated with programming solely through side-effects, a ...

Uncovering the Proliferation of Memory Leaks in NodeJS

My ExpressJS Server is set up like this: var express = require("express"), app = express(); app.use(express.static(__dirname)); app.listen(1050); Upon starting the server, it consumes 20MB of v8 heap memory. However, if I reload the page every seco ...

The browsers Firefox and Internet Explorer are unable to perform ajax requests

Currently, I am utilizing jQuery version 3.3 in conjunction with the following Ajax script: <script type="text/javascript"> $(document).ready(function(){ $("form").submit(function(){ $.ajax({ url: 'msgs.p ...

What is the best way to align the navbar items at the center in Bootstrap when mx-auto is not working?

<nav class="navbar navbar-expand-sm navbar-light"> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls ...

Discovering the current active link and anchor tag details using jQuery in an unordered list

I am currently utilizing jQuery to work with 4 anchor tags inside an unordered list, and I would like to be able to determine the "current active link" when a user performs a search. This way, I can execute my query based on the selected anchor tag. How ca ...

HTML5 failing to load button

<!DOCTYPE html> <html> <head> <title>Greetings Earthlings</title> <script src="HelloWorld.js" /> </head> <body> <button onclick ="pressButton()" >Press Me!</button> ...

The functionality of ng-show becomes compromised when used in conjunction with ng-animate

Once the animate module is activated, the ng-show functionality seems to stop working. Even though the default value for the ng-show expression is set to false, the element is still displayed and the ng-hide class is not being applied. However, if I deac ...

What is the process for capturing a window screenshot using Node.js?

I am currently conducting research to discover a way to capture a screenshot of a window using Node.js. I have been attempting to achieve this with node-ffi, but I am facing some difficulties at the moment: var ffi = require('ffi'); var user32 ...

Guide to configuring Nodemailer with Angular 4

I've been searching everywhere for a tutorial that can walk me through the steps of setting up nodemailer in Angular 4. I'm feeling lost about where to place the ts code snippet from nodemailer's website: 'use strict'; const nodem ...

Filtering a deeply nested object based on an array of keys in JavaScript

I have received an object and an array. My task is to filter the object so that it only contains nodes where either the key or the value matches with the keys provided in the array. For example: Array containing keys - ['student1', 'STU&apo ...

Using Typescript: Defining a function parameter that can be either of two interfaces

While browsing through this specific question, I noticed that it was somewhat related to my current issue, although there were notable differences. In my scenario, I have a function named parseScanResults which accepts an object as its argument. This obje ...

Unexpectedly, the API is displaying an empty array despite the fact that there is data

Hi all, as a beginner I am currently working on building a React app with an Express backend. I have successfully created an API that retrieves data from MongoDB. When examining the data in MongoDB using Robo3T, I noticed that the options array appears to ...

Place the menu on top of the div by adjusting the z-index

Hey there, I'm working on a project and I need help with an issue. You can check out the example page here: If you try to open the menu and click between "list" and "new ads", you'll notice that you're actually clicking on the dropdown se ...

The <a href="#divtagid"> link is incapable of triggering the opening of the div tag when called from JavaScript

I need help with displaying the content of a div with the id "hello" in maindiv when clicking on the href "Click Here", but it's not working as expected. Here is the code I have: $(document).ready(function() { var newhtml = '<a href="#he ...

Encountering an error stating that 'coordinates should consist of an array with two or more positions'

Utilizing turf.js to generate a line depicting the path of an individual while their location is tracked. An array of coordinate arrays resembling Turf.js (lineString) is causing this error: Uncaught Error: coordinates must be an array of two or more posi ...

Executing tasks in a While loop with NodeJS and attaching actions to a Promise

I am relatively new to incorporating Promises in NodeJS. My current task involves creating a promise dynamically with multiple actions based on the characters found in a string. //let actions = []; getPromise = get(srcBucket, srcKey); // Retrieve the imag ...

Design a dynamic dropdown feature that is triggered when the chip element is clicked within the TextField component

Currently, I am facing difficulties in implementing a customized dropdown feature that is not available as a built-in option in Material UI. All the components used in this project are from Material UI. Specifically, I have a TextField with a Chip for the ...

Why am I unable to locate my personalized module?

I've encountered an issue with my npm module not being found in my sample script after publishing it. Here is the link to my module: https://www.npmjs.com/package/kong-hmac https://github.com/y-zono/kong-hmac-js Here's what I have tried: $ m ...

An abundant array of options spread across numerous dropdown menus

I have been tasked by a client to do something new that I haven't done before, so I am seeking the best approach. The project involves creating a form with three dropdown menus where users can select from thousands of options. For instance: Users mu ...

Populating a many-to-many relationship in Mongoose using uni-directional references

These are the schemas I'm using to represent a many-to-many relationship: var GenreSchema = new Schema({ name: {type: String}, }); mongoose.model('Genre', GenreSchema); var MovieSchema = new Schema({ title: {type: String}, genres ...