Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Node.js incorrect path problems

Node.js incorrect path problems

Problem

When I do fs.readFile(file... I get this error:

{ [Error: ENOENT, open 'foundation/css/normalize.css']

errno: 34,

code: 'ENOENT',

path: 'foundation/css/normalize.css' }

Do I have to route my path in relation to the HTML file they are linked from, or in relation to the location of the node.js files running the server and router?

And no, I'm not using express and "use express" is not an acceptable answer.


Edit

@BenjaminGruenbaum's last comment. I'm not sure what you mean by my HTTP response, but my code logic is as follows: I load my index.html via the URI and when it readFile the document it then recursively goes through every other linked file. I have a script that breaks it down to properly display the right Content-Type. I'll include it here because its too long for a comment:

function route(handle, pathname, response, request){
// console.log("About to route a request for " + pathname);

if (typeof handle[pathname] === "function"){        
    handle[pathname](response, request);
    } else {

    // console.log('NON HTML');
    // console.log(pathname);

    var file_path = "";
    var mimes = {
        'css':  'text/css',
        'js':   'text/javascript',
        'htm':  'text/html',
        'html': 'text/html',
        'png': 'image/png',
        'jpg': 'image/jpg',
        'jpeg': 'image/jpeg'
    };

    // parses the url request for a file and pulls the pathname
    var url_request = url.parse(request.url).pathname;      
    // console.log('URL REQUEST');
    // console.log(url_request);

    // finds the placement of '.' to determine the extension    
    var tmp  = url_request.lastIndexOf(".");
    // determines the extension by uing .substring that takes everything after '.'
    var extension  = url_request.substring((tmp + 1));
    // console.log('EXTENSION');
    // console.log(extension);


    //set path of static pages
    if (extension === 'css' || extension === 'js' || extension === 'htm' || extension === 'html' || extension === 'png' || extension === 'jpg' || extension === 'jpeg'){

        // console.log('LOAD STATIC PAGE');
        file_path = url_request.replace("/", "");
        // console.log(file_path);
        // console.log();
    }

    //load needed pages and static files
    fs.readFile(file_path, function (error, data){
        if(error){
            // console.log("AM I DEAD");

            // console.log(error);
            // console.log();
            response.writeHeader(500, {"Content-Type": "text/html"});  
            response.write("

FS READ FILE ERROR: Internal Server Error!

"); } else{ response.writeHeader(200, {"Content-Type": mimes[extension]}); console.log('SHOULD BE SUCCESS') console.log(mimes[extension]); // console.log(data); response.write(data); } response.end(); }); response.end(); } } exports.route = route;

As you can tell by how many places I have console.log() I can track it all the way to the bottom It fails here:

else{
            response.writeHeader(200, {"Content-Type": mimes[extension]});  

            console.log('SHOULD BE SUCCESS')
            console.log(mimes[extension]);
            // console.log(data);
            response.write(data);
        }

I apologize for the somewhat sloppy indentation.


Final edit:

The eventual resolution to this stupid situation can be found at my following question here: Node.js - Resource interpreted as Script but transferred with MIME type text/plain

Much thanks to @BenjaminGruenbaum for putting up with a Noob

Problem courtesy of: JDillon522

Solution

You would have to use the path relative to your NodeJS files (the entry point more specifically) and not your client-side HTML.

This is just like it works in bash scripts, in Python and pretty much in any other language with a sane API that makes sane calls to the native file system functions in the operating system.

In fact, the client side HTML has little to do with pathing in Node - in Node's stand point it's just text being sent in an HTTP response to the client. It's not aware of the server's inner file system at all.

(Also, I don't see how anything like express would even be any help anyway)

Solution courtesy of: Benjamin Gruenbaum

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Node.js incorrect path problems

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×