3
\$\begingroup\$

I'm working on a Node.js HTTP server on my iPhone:

var fs = require("fs");
var http = require('http');
const port = 8080; 
var readFile = function(name, response) {
    var read = function(err, dat) {
        var rv = null;
        var nope = "./redirect.html";
        if (err) {
            if (name == nope)
                rv = "INTERNET!";
            else {
                rv = readFile(nope, response);
                return;
            }
        }
        else rv = dat;
        response.end(rv);
    }
    fs.readFile(name, "utf-8", read);
}
var client = function(request, response) {
    path = "." + request.url;
    file = readFile(path, response);
}
var server = http.createServer(client);
server.listen(port);

redirect.html:

<script language = "javascript">
window.location = "/index.html";
</script>
Bad request.
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$
    if (err) {
        if (name == nope)
            rv = "INTERNET!";
        else {
            rv = readFile(nope, response);
            return;
        }
    }
    else rv = dat;

Omitting {} can easily go sideways especially if you mix it like in the example above. I suggest to always use them.

Even if it's just

else { rv = dat; }

they greatly helps to see where the if/else ends.

var nope = "./redirect.html";

Since you doesn't seem to change it anwhere, I'd make it a constant too.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.