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.
asked Oct 31, 2016 at 23:30
\$\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.

answered Nov 1, 2016 at 9:54
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.