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.