A simple nodejs tutorial. Lets start from implement the server.js
var vhttp = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("hello world!");
response.end();
}
vhttp.createServer(onRequest).listen(3000);
console.log("Server has started on port :3000.");
goto localhost:3000 great! we have our page!
<HTML>
<HEAD>
<TITLE> hello javascript </TITLE>
</HEAD>
<BODY>
<h1> SIMPLE KANBAN </h1>
</BODY>
</HTML>
-
new
// we now need to read index.html and serve it to our client var fs = require("fs"); var page = ""; fs.readFile( __dirname + "/index.html", function (err, data) { if (err) { throw err; } page = data.toString(); }); // ------ add these line at the front of
-
changes
response.writeHead(200, {"Content-Type": "text/html"}); response.write(page); // ------ add these line in onRequest function
Now we have our page! Lets add some route
-
new
var url = require("url"); // ------ add this line at the beginning var pathname = url.parse(request.url).pathname; console.log("Request for " + pathname + " received."); if (pathname == "/") { response.writeHead(200, {"Content-Type": "text/html"}); response.write(page); response.end(); } else if (pathname == "/message") { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("You are on /message !"); response.end(); } // ------ add these line in onRequest function
-
new
<form id="msg" method="POST" action="#"> <label> name </label><input type="text" id="username" name="username" /> <label> message </label><input type="text" id="message" name="message" /> </form> <button onClick="SubForm()"> call </button> <script> function SubForm (){ alert(" you called me~~"); } </script> // ------ add these line in index.html, after <h1> SIMPLE KANBAN </h1>
-
next
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> function SubForm (){ $.ajax({ url: "/message", type: "post", data: $("#msg").serialize(), success: function(){ console.log("worked"); } }); }; </script> // ------ change the content of SubForm()
-
new
var qs = require("querystring"); // ------ add this line at the beginning if (request.method == "GET") { response.writeHead(200, {"Content-Type": "text/plain"}); response.write("You are on /message !"); response.end(); return; } else if (request.method == "POST") { var body = ""; request.on("data", function (data) { body += data; }); request.on("end", function () { var post = qs.parse(body); console.log(post["username"]); console.log(post["message"]); response.writeHead(200, {"Content-Type": "text/plain"}); response.write("done"); response.end(); }); } // ------ change the content of onRequest function `else if (pathname == "/message")`
-
new
<button onClick="update()"> update </button> <div id="hello"> </div> // ------ add these line below <button onClick="SubForm()"> call </button> <script> function update (){ $.get("/message", function(data) { $("#hello").empty(); data = JSON.parse(data); for(var i in data) { for(var j in data[i]) { $("#hello").append("<div>" + i + " says " + data[i][j] + "</div>"); } } console.log(data); }); }; </script> // ------ add these line within <script>
-
new
var messages = {}; // ------ add this line at the beginning if (messages[post["username"]] == undefined) { messages[post["username"]] = []; messages[post["username"]].push(post["message"]); } else { messages[post["username"]].push(post["message"]); } // ------ add these line within
-
change
// on GET message response.write(JSON.stringify(messages));
-
new
<!-- Include the polyfill --> <script src="//cdnjs.cloudflare.com/ajax/libs/web-animations/2.3.1/web-animations.min.js"></script> <!-- Include Animatelo --> <script src="//cdnjs.cloudflare.com/ajax/libs/animatelo/1.0.3/animatelo.min.js"></script>
-
change
// in update function window.animatelo.bounce("#hello");