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

Generating an interactive table using JSON with Angular 5

Can a dynamic table with dynamic columns be created based on a JSON object using Angular 5? If yes, how? The API response includes the following JSON: { "ResponseStatus": true, "ResponseData": [ { "Parent": "Company 1", ...

Incorporating a "CC" recipient within a Node email document

In my Node backend, I am using an npm package called "eml-format" to create eml files. I am currently facing a challenge in determining the syntax that should be used to designate a "cc" on an email. The example provided by the package for building the eml ...

Creating secure private messaging functionality with Socket.io, Express.js, Node.js and Mongoose

I'm currently facing difficulties with implementing socket.io to create a chat application that connects users who are logged into my platform. Although I successfully developed a private chat app following a beginner course, the users involved in the ...

"Using the map function in Javascript to iterate through an array and then implementing

I am working on a script that involves an array of the alphabet along with two sets of values. The goal is to determine if a given value falls within the range specified by these two values and then print out the corresponding letter from the alphabet. H ...

Displaying the user's username upon logging into a website is a simple yet effective

I have successfully developed a simple web page using PHP and HTML. However, I am facing an issue in displaying the username after login. Additionally, I would like to hide the login and signup sections after successful login. Can anyone provide assistance ...

Why isn't Meteor.call functioning in the stub?

As I delve into the realm of async JavaScript coding, I have encountered a gist that has left me puzzled: https://gist.github.com/dariocravero/3922137 Specifically within client_save.file.js - there are parts of this code snippet that baffle me: fileRead ...

Is the click count feature malfunctioning?

My goal is to track every mouse click made by the user, so I created a function for this purpose. However, it seems that the function is only counting the first click. To store the count values, I have created a variable. <!DOCTYPE html PUBLIC "-//W3 ...

The click() function is not properly functioning for the input tag in selenium automation when using nodejs

I am attempting to click on this specific element, but despite no errors, the click action is not being executed. <input class="btn-add-cart button js-form-submit form-submit" data-drupal-selector="edit-add" type="submit" i ...

Safari Browser does not currently offer support for MediaRecorder functionality

[Log] Webcam permission error Error: MediaRecorder is not supported I am facing an issue while trying to record audio. The Chrome browser allows audio recording without any problem, but Safari is throwing an error. global.audioStream = await navigator.m ...

The contents within the div element exceed the width of its parent element

Hey there, I'm currently working on a Rails application to showcase some links in a horizontal layout. The links are indeed displaying horizontally without wrapping, just as intended. However, I've noticed that the containing div is wider than th ...

Ways to utilize jquery to limit the length of an editable div and prevent it from exceeding our specified restriction

Imagine a scenario where you have the following code snippet: <div id="editing" contenteditable onclick="document.execCommand('selectAll',false,null)">Put text here...</div> In this situation, let's say you want to impose a r ...

Using HTML and CSS to Position Text Within an Image

Is there a way to position an image next to specific points on the screen so that it remains in the same place regardless of screen size? Here is what I am trying to achieve: https://i.stack.imgur.com/A5cop.png Using HTML, this is my desired setup: < ...

Acquiring the SQL queries using the Node pg module

I'm currently using the pg module for node, and I'm encountering an issue where my queries sometimes return empty results when accessing rows from different tables. During testing, this error makes it difficult to identify the problem. To make tr ...

Leveraging generics within TypeScript

I have developed a class in TypeScript that uses generics. export class ModelTransformer { static hostelTransformer: HostelTransformer; static fromAtoB(instance: T): U { if (instance instanceof HostelType) { return ModelTrans ...

CSS does not have the capability to style child elements

Having trouble changing the text for child elements when applying CSS classes to parent elements. Is there a specific reason why this is happening? Take a look at my current code: <Box //not affecting all child elements ...

Automatic keyboard suggestions not working for HTML search input using Ajax

I am currently working on a PHP web application that uses a MySql database. I have implemented a search suggestion box using ajax. The issue I am facing is that the suggestions can be selected with the mouse and auto-completed, but not when using the keybo ...

What's the most effective method to incorporate additional events into this element using the conditional operator?

Looking for help with this code snippet: <span role="link" tabindex="0" :class="tabDetails.showPayment ? 'link' : ''" @click="tabDetails.showPayment ? cTab('payments') : null" ...

Is there a way to adjust the size of text to perfectly fit within a fixed size div?

I find this scenario quite common, but I haven't come across any solutions for it: Let's say there is a fixed-width div that displays dynamically changing numbers. How can we adjust the font size so that larger numbers fit well within the fixed ...

ngModel is not taken into account when processing form data

Attempting to make use of a dynamic form in AngularJS, the code snippet below has been utilized: <dynamic-form template="formTemplate" ng-model="formData" ng-submit="processForm()"> </dynamic-form> The controller script inc ...

Discovering Unconventional Columns Through Sharepoint REST Api Filtration

I am working on recreating SharePoint's front end in my app and want to add columns to my table just like a user would in SP. The challenge I am facing is determining which columns were custom generated by the user rather than standard ones. Although ...