Issue with res.sendFile not functioning as expected

I'm encountering difficulties with using express. I am trying to launch by typing

localhost:3000/timer -> timetimer.html
, but it's not functioning as expected.

server.js:

var express = require('express');
var path = require('path');
var indexRouter = require('./routes/index');
var app = express();

app.use(express.static(path.join(__dirname, 'public')));
app.use('/', indexRouter);
app.use(function(req,res,next){
    res.status(404).send('Unknown Request');
    next();
});

app.listen(function () {
  console.log('Server listening on port 3000!');
});

module.exports = app;

index.js:

var express = require('express');
var path = require('path');
var router = express.Router();

router.get('/main', function(req,res){
    res.sendFile('/main.html', {root: path.join(__dirname,'../public')});
});

router.get('/timer', function(req,res,next){
    //res.sendFile('public/timetimer.html', {root: path.join(__dirname,'../public')});
    res.sendFile(__dirname + "/timetimer.html")
});


module.exports = router;

Folder structure:

https://i.sstatic.net/8Xhnk.png

The code launches successfully, showing that server.js is listening on port 3000. However, when I enter localhost:3000/timer in the browser, the site cannot be reached.

Answer №1

Make sure to include the port number in your app.listen function.

Here is an example:

app.listen(3000, function () {
  console.log('Server listening on port 3000!');
});

Answer №2

__dirname represents the directory path of the current module.

In this case, it pertains to the routes folder.

However, since timetimer.html is not located in the routes folder, you are providing an invalid path.

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

Troubleshooting issues with Bootstrap's responsiveness configuration

I've been working on creating a responsive user login page using Angular 5. It looks great on full-screen display. https://i.sstatic.net/YQrL5.png However, when I resize the browser window, the responsiveness seems to break. https://i.sstatic.net/4 ...

Error in Meteor: TypeError occurs when trying to use FS because it cannot read the property 'Collection' as it is undefined

Currently grappling with a challenge in my /server/main.js file while using Meteor 1.6 and AngularJS (Angular 1). Attempting an import as shown below: import { FS } from 'meteor/cfs:filesystem'; Although Meteor can resolve it without issue, the ...

Ways to emphasize a distinct cell within a UI Grid by utilizing the cellClass feature

I am struggling with a requirement where I need to highlight a specific cell in a grid using its row and column numbers. However, in the current setup, when I scroll through the grid, other cells are also getting highlighted. It seems like I am not graspin ...

Beginner Attempting to Create a JavaScript Age Calculator

As a beginner in self-learning, I've recently started diving into the world of JavaScript. Currently, I am facing a challenge with an assignment from an online code camp that I just can't seem to figure out. Despite grasping the basics, I struggl ...

Incorporating an external JavaScript or CSS file into a Java web application at runtime

I am looking to dynamically add an external JavaScript or CSS file in my test.html file. Although I am aware of a trick that involves using jQuery like this: $(”).appendTo(‘head’).attr({ rel: ‘stylesheet’, type: ‘text/css’, href: ‘**ht ...

Angular JS: Grabbing Text from a Specific div and Copying it to Clipboard

I recently developed a Random Password Generator using Angular JS. Here is how the output appears in the application: <div data-ng-model="password" id="password"> <input class="form-control" data-ng-model="password" id="password" placeholder=" ...

The issue of recursion not functioning properly with ng-include and ng-repeat arises beyond the initial level

I am trying to iterate through an object in order to fill a table dynamically. To achieve this, I am utilizing ng-include and ng-repeat to loop through the object and generate a new <tr> for each element. However, I am encountering issues after the ...

Utilizing the Calc() method within CSS specifically for adjusting the padding on the left and top sides

I am currently working on a website using HTML and SASS. My design includes a fixed header and aside sections. header{ width: 100%; box-shadow: 0 0 4px rgba(0,0,0,.25); position: fixed; z-index: 5; top: 0; ...

Step by step guide to creating individual counter sections

I have set up a counter section where the numbers go from 0 to a specific value. However, currently all three counters start counting simultaneously. Is there a way for the first counter to count up first, then once it's done, move on to the second c ...

Iterating through a jQuery function to increment value

I have encountered an issue while trying to calculate the total value from an array of form fields. The problem lies in how the final value is being calculated on Keyup; it seems that only the last inputted value is being added instead of considering all t ...

Encountering an issue with Karma: Unable to open browser with karma-webdriver-launcher

I attempted to use Karma along with karma-webdriver-launcher and karma-selenium-grid-launcher in order to launch Chrome/Firefox for test execution, but I am encountering issues with browser opening. Could someone kindly provide me with a working code snip ...

The Note-Info Form is not automatically populating when clicking on the Edit button

After clicking the edit button, the note information does not automatically populate as expected. I have reviewed everything and tried various methods, but the issue persists consistently. useEffect(() => { const fetchNote = async () => { const ...

The footer section of my website seems to have gone missing in the HTML/CSS coding

I'm having trouble with my website footer not displaying. I've tried using "position: absolute; top: 50px; left:100px;" in my HTML/CSS, but it's not working. Can anyone help me fix this issue? Here is a link to the code I'm working on: ...

What steps do I need to take in order to integrate my unique landing page design into Wordpress

Currently, I am involved in a project that utilizes Node.js on Docker. As part of this project, I have developed a landing page using Bootstrap. In the future stages of the project, our team plans to incorporate Wordpress as a content management system ( ...

excess spillage occurring within the table's td elements

Is there a way to apply the overflow property to a cell within a table? http://jsfiddle.net/e8Bxj/ table { width:100px; height:100px; } td.content { overflow:auto; } <table> <tr> <td>hmm</td> </tr& ...

Ways to enhance an image by zooming in when the user reaches a designated area on the webpage

I have implemented a feature where an image zooms in to letter L when the user scrolls on the page. However, I want this zoom effect to occur only when the user reaches a specific section of the site, rather than immediately when the image loads. In my exa ...

A guide on connecting static files in Django when the HTML context includes the file path

Here's the content of my views.py file: from django.shortcuts import render def page(request): css = 'temp/css.css' return render(request, 'temp/index.html', {'css': css}) and this is the content of templates/t ...

Tips for dealing with event bubbling in React

Looking for a way to add an onBlur event to the left panel so that it closes when the user clicks on the right panel. I attempted using onMouseLeave but the closing animation isn't as smooth as desired. Additionally, I'd like for users to close t ...

Troubleshooting problems with redirecting users after logging in using React and Express with local

There seems to be a small missing piece that I haven't quite grasped yet when it comes to React. My setup involves a react frontend connected to an express/mongoose backend. After logging in, an 'authToken' is stored in the local storage, wh ...

Having trouble with implementing forEach in Google Script

Hey there, I'm having a syntax error in my GoogleScript. This is actually my first script ever, so I'm a bit lost as to why it's not working. The main goal is to extract data from a Google sheet and use it to create labels based on a documen ...