SimonC
SimonC

Reputation: 1618

Response not ending

I'm creating a frontend application for one of our projects for user, cluster, (single) node management, etc.

I've decided to create the frontend using HTML and JavaScript, and NodeJS for server-side functionality.

I've created the webserver, can handle both defined routes and also files; and I've decided to split operations in to functions. From what I've gathered about JavaScript, parameters are called by value if they're not objects, and by reference if they are objects?

If this is the case, why can I not end() my request from an external function? And if I can, how do I go about this?

Thanks!

Code is below for reference.

// Define server functionality
    if (request.method === "GET") {
        // All GET functions/routes go here
        if (routes[request.url]) {
            // Route was found!
            // Execute handler function
            routes[request.url](request, response);
        } else {
            // No such route found in the hashtable
            // Check if it's a file
            var filePath = __dirname + request.url;
            if (fs.existsSync(filePath)) {
                // It's a file and was found!
                //zOMG!!!1!elf
                var mimeType = mime.contentType(path.extname(filePath));
                var content = null;
                if (mimeType.indexOf("text") > -1) {
                    // File contains text
                    content = fs.readFileSync(filePath, "UTF-8");
                } else {
                    // File is binary
                    content = fs.readFileSync(filePath);
                }
                //console.log(mimeType); // Output MIME type of each requested file
                response.statusCode = 200;
                response.setHeader("Content-Type", mimeType === false ? "application/octet-stream" : mimeType);
                response.end(content);
            } else {
                // File not found!
                console.log(`File ${filePath} was NOT FOUND!`);
                console.log("Cannot serve null!");
                error404(request, response);
            }
        }
    }

function error404(request, response) {
    "use strict";

    response.statusCode = 404;
    response.end();
}

Upvotes: 1

Views: 65

Answers (1)

SimonC
SimonC

Reputation: 1618

Found Answer myself

Typical symptom of too little sleep. I forgot to restart the server after modifying the code.

facepalm

Upvotes: 1

Related Questions