Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:30:48+00:00 2026-05-28T23:30:48+00:00

yesterday i’ve played a little with Node.js and my first idea is making a

  • 0

yesterday i’ve played a little with Node.js and my first idea is making a simple web server that load an html page with some js files in this way:

var http = require('http'),
    fs = require('fs'),
    path = require('path');

http.createServer(function(request, response) {
    console.log('request starting for: ' + request.url);
    var filePath = path.join('.', request.url);
    if (filePath === './') {
        filePath = './aPage.html';
    }

    path.exists(filePath, function(exists) {
        if (exists) {
            var extname = path.extname(filePath);
            var contentType = 'text/html';
            switch (extname) {
            case '.js':
                contentType = 'text/javascript';
                break;
            case '.css':
                contentType = 'text/css';
                break;
            }

            fs.readFile(filePath, function(error, content) {
                if (error) {
                    response.writeHead(500);
                    response.end();
                }
                else {
                    response.writeHead(200, {
                        'Content-Type': contentType
                    });
                    response.end(content, 'utf-8');
                }
            });
        }
        else {
            console.log('Something goes wrong ;(');
            response.writeHead(404);
            response.end();
        }
    });
    console.log('Server running!');
}).listen('8080', '0.0.0.0');

and everything works.

I decided to put this js script in a subdirectory, modify the lines in:


...

var filePath = path.join('..', request.url);
    if (filePath === '../') {
        filePath = '../aPage.html';
    }
...

but path.exists() fails to check the existence of html page and others files.

Could you please tell me what’s my fault (I thought that was only that trivial change)?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:30:49+00:00Added an answer on May 28, 2026 at 11:30 pm

    My guess is that you are trying to directly run the js script from the parent folder instead of from the subdirectory.

    for example: if you are in directory foo and your server.js is in subdir bar,
    then if you run node bar/server.js, then .. will point to the parent of foo, instead of parent of bar, that’s why the file is not found.

    foo
      +---bar
      |     +----- server.js
      +---- aPage.html
    

    You may try to cd into bar and run node server.js.

    or change the ../aPage.html in the script to be __dirname/../aPage.html.

    PS: you can use path.resolve to get the absolute path.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Yesterday I discovered an odd bug in rather simple code that basically gets text
Yesterday I added a custom MembershipProvider to an ASP.NET web application, but when I
Yesterday, my supervisor told me that tmp-file-based session should be THE answer to session
Yesterday I felt in big issue that happens with all my projects. After I
Yesterday I had to parse a very simple binary data file - the rule
Yesterday I notice that some thing kept writing with 1.48M/s bursting speed every 30s
Yesterday, when I had an error or warning a little icon would appear next
Yesterday I made myself a new my first App ID, all Certificates and downloaded
Yesterday I had a team leader of another team say that they took a
Yesterday I found that I needed to add self to an object as it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.