hdtsai / simple-nodejs-example

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Table of Contents

  1. Intro
  2. build steps
    1. first http server
      1. server.js
      2. on browser
    2. first web page
      1. index.html
      2. server.js
      3. on browser
    3. lets add some route
      1. server.js
    4. post function
      1. index.html
      2. server.js
    5. communication part
      1. index.html
      2. server.js
    6. final add some animation!
      1. index.html
  3. Enjoy

Intro

A simple nodejs tutorial. Lets start from implement the server.js

build steps

first http server

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).listed(3000);
console.log("Server has started on port :3000.");

on browser

goto localhost:3000 great! we have our page!

first web page

index.html

<HTML>
  <HEAD>
	<TITLE> hello javascript </TITLE>
  </HEAD>
  <BODY>
	<h1> SIMPLE KANBAN </h1>
  </BODY>
</HTML>

server.js

  1. 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();
    });
    
  2. changes

    response.writeHead(200, {"Content-Type": "text/html"});
    response.write(page);
    

on browser

Now we have our page! Lets add some route

lets add some route

server.js

  1. new

    var url = require("url");
    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();
    }
    

post function

index.html

  1. 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>
    
  2. next

    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
      function SubForm (){
    	$.ajax({
    	  url: "http://hare1039.nctu.me:5900/message",
    	  type: "post",
    	  data: $("#msg").serialize(),
    	  success: function(){
    		console.log("worked");
    	  }
    	});
      };
    </script>
    

server.js

  1. new

    var qs = require("querystring");
    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();
      });
    }
    

communication part

index.html

  1. new

    <button onClick="update()"> update </button>
    <div id="hello"> </div>
    
    function update (){
      $.get("http://hare1039.nctu.me:5900/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);
      });
    };
    

server.js

  1. new

    var messages = {};
    
    if (messages[post["username"]] == undefined) {
      messages[post["username"]] = [];
      messages[post["username"]].push(post["message"]);
    } else {
      messages[post["username"]].push(post["message"]);			
    }
    
  2. change

    // on GET message response.write(JSON.stringify(messages));

final add some animation!

index.html

  1. 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>
    
  2. change

    // in update
    window.animatelo.bounce("#hello");
    

Enjoy

About


Languages

Language:HTML 50.3%Language:JavaScript 49.7%