0

I have read that Javascript can be used for back-end of a website, so like server side scripting. I have seen here that nodejs is accepted by Google cloud storage but using node js which confuses me as I don't know how to use it with a website in a text editor e.g. Brackets.

I have been trying to create a server and use the console to show that someone has connected:

var http = require("http");
var fs = require("fs");
var lemon = 2;

function send404Response(response) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("Error 404");
response.end;
}

function onRequest(request, response) {
if (request.method == 'GET' && request.url == '/'){
      response.writeHead(200, {"Content-Type": "text/html"});
    //sends back a readable stream
    fs.createReadStream("./index.html").pipe(response);
} else {
    send404Response(response);
}

}
http.createServer(onRequest).listen(8080);
console.log("Server is now running...");

'require' was used before it was defined. var http = require("http"); is the first error and I don't know what to do nor can I find any help except that I'm not supposed to do require for a website.

In review please answer these questions:

  • how do you use Javascript for server side scripting and some tutorials would be useful.

    • how to use nodejs with a website, since its a command line.
    • what is the issue with the code?
  • Also which port am I supposed to use when using brackets live update?

Please if there is an issue with the question please leave a comment I have tried to follow the rules.

newGuy
  • 13
  • 6
  • Check out [this article](https://medium.com/google-cloud/running-node-js-on-app-engine-e49e9bb31d3b). – skiilaa Jul 05 '17 at 16:40
  • You use Node.js for the server, not the client. – SLaks Jul 05 '17 at 16:43
  • I was using brackets which does not like node js hence there was an issue with the 'require', but there was no effect on the function of the website. – newGuy Aug 03 '17 at 12:50

0 Answers0