Unable to determine why node.js express path is not working

const express = require("express");
const app = express();

app.use(express.static("public"));

var dirname = __dirname;
app.get("/:lang/:app",function(req,res){
  console.log(req.params.lang + " " + req.params.app);
  res.sendFile(dirname + "/index.html", { layout: false });
});

app.listen(process.env.PORT || 3001,function(){
  console.log("started");
});

I have encountered an issue with my index.js code. When I try to access a specific route like "/:lang/:app", the HTML and CSS files do not load properly and I receive the following error message: Error: ENOENT: no such file or directory, stat 'C:\Users\directory\calculator-release\index.html'. Can anyone help me fix this problem?

const express = require("express");
const app = express();

app.use(express.static("public"));

var dirname = __dirname;
app.get("/",function(req,res){
  //console.log(req.params.lang + " " + req.params.app);
  res.sendFile(dirname + "/index.html", { layout: false });
});

app.listen(process.env.PORT || 3001,function(){
  console.log("started");
});

This revised code should work without any issues.

Answer №1

It appears that the issue lies with the incorrect file path while attempting to retrieve the html file.

Correction: __dirname indicates your current working directory. Therefore, instead of using

dirname + "/index.html"
, you may want to try
dirname + "/public/index.html"

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

Is it possible to simultaneously install a package both locally and globally in a Node.js environment?

Currently, I am engaged in a project where I have utilized nodemon as a globally installed tool for development purposes. To automate the process of running a nodemon script, I modified my package.json file by adding a specific script. The setup looks lik ...

Getting the total number of child elements in a web page using Selenium web-driver with Node.js

I've been looking everywhere, but I can't seem to find the answer Here's my HTML code: <form id="search_form_homepage" > ... <div class="search__autocomplete" style="display: block;"> &l ...

Result of a callback function

Having trouble returning a value for form validation using a callback function. It's not working for me... <form action="loggedin.php" onsubmit="return test(valid)" method="post"> function test(callback) { var k = ""; var httpRequest = ...

Linking Two HTML Components in Angular 4 with Identical Values

Recently, I've started working with Angular and encountered an issue. In a table row, the value item.md_id is bound like this: <tr *ngFor="let item of driverData"> <td class="align-right" id="md_id" [(ngModel)]="item.md_id" name="driverId ...

Verify that the lower limit is not higher than the upper limit set in an Angular application

In my Angular application, I have two input fields for minimum and maximum values. I want to display an error message if the user enters a minimum value greater than the maximum value provided, or the default maximum value if none is provided. The form is ...

Having difficulty accessing the value of a table td element in HTML using a jQuery selector

When creating a table, I utilize ng-repeat to generate table rows. Whenever the dropdown changes, a function is triggered which applies certain conditions. Based on these conditions, an object is added to an array that is bound to a scope variable. Here i ...

The flex-direction property is not behaving as anticipated in the Chrome browser

Hi everyone, I really need some assistance with my code. The flex-direction property is not working for me no matter what I try. I've tested it on both Chrome and Microsoft Edge, but it displays the same result. I've been stuck on this for the pa ...

The functionality of $watch in AngularJS is not meeting the desired outcomes

Within my controller, I am looking to receive notifications when the value of a certain variable changes. Specifically, I want a function to be triggered whenever this variable is updated. To achieve this, I am utilizing the $watch method in AngularJS. Her ...

Position a <div> element in the center of another <div> element

Link to code: https://jsfiddle.net/z4udfg3o/ My goal is to center the "caixa" divs within the "produtos" div. Initially, I achieved this by using exact values with margin-left. However, I now want it to be responsive across different screen sizes, so I ha ...

The video auto plays in a pop up window, but if the pop up doesn't appear, I can still hear the audio

I recently added a pop-up to my website and attempted to include autoplay functionality. However, I encountered an issue where the sound would still play even if the pop-up did not appear upon refreshing the page. How can I prevent the autoplay when the po ...

What is preventing typescript from inferring these linked types automatically?

Consider the following code snippet: const foo = (flag: boolean) => { if (flag) { return { success: true, data: { name: "John", age: 40 } } } return { success: false, data: null } ...

Creating credentials for the npm registry URL

Recently, I encountered an issue while trying to use npm to install a package from a specific URL: . To address this problem, I took the following steps: Set strict-ssl to false using the command npm config set strict-ssl false. Modified the registry by ...

An unusual issue encountered while utilizing jQuery toggle: a straightforward illustration

Attempting to create a partial fade effect using toggle for toggling opacity values when clicking an element, but encountering an issue where nothing happens on the first click. The HTML code: <html> <head> <script type="text/javascript" s ...

How can I load a separate component.html file using a component.ts file?

Hey there, I'm a beginner with Angular and I have a question about loading a different home.component.html file from a method within books.component.ts. Here's the code snippet from my books.component.ts file: import { Component, OnInit } from ...

Present location of current page

I utilize angularJS to create a web page with the utilization of ngView for a single page and multiview. Is there a way to display the current page's location, such as Home/abc/dcv? On this website https://docs.angularjs.org/guide/, the page location ...

Issue with Tabulator: The top and bottom calculations do not shift position when a column is toggled. Seeking a solution to toggle a column and refresh the table without losing the current

My main objective is to toggle the visibility of toggles while maintaining consistent formatting. However, I currently face an issue where using a filter achieves this but results in losing the current scroll position of the tabulator, which is not accepta ...

The html input box's ability to handle overflow situations

Currently, I am working on creating a form that allows users to update data. <dl> <dt>Medical Needs:</dt> <dd class="med_needs" style="height:60px;overflow:auto"> <input type = "text" id="med_needs"name ="med_nee ...

Error: Uncaught Type Error - Attempted to access property 'then' of an undefined variable. This issue occurred when attempting to use a

I attempted to use the code below, considering that node operates asynchronously and in order to execute my code sequentially I added a then function. However, I encountered the error TypeError: Cannot read property 'then' of undefined. The code ...

New feature alert! Introducing the Mentio JS menu now available at the bottom of the webpage

I am currently working on creating a Twitter-style @mention feature using Angular JS and a library called MentioJS. One issue I encountered is that after dynamically adding content to the page, a mysterious menu appears at the bottom of the page. This pro ...

Determine whether a child node is an element or a text node using JavaScript

I am experiencing an issue with the childNodes property. Here is the scenario: <ol> <li>Coffee</li> <li>Tea</li> <li>Coca Cola</li> </ol> //childNodes.length = 7 However, <ol><li> ...