LEARNING MEAN STACK: NODE JS – 101

MEAN
Node JS: Web Server Part
  1. Creating a Simple Web Server
  1. var http = require('http');
  2. http.createServer(function (req, res) {
  3.     res.writeHead(200, {'Content-Type': 'text/plain'});
  4.     res.end('Hello World How are you !');
  5. }).listen(8080,'localhost');
Executing the above code in Node will create a web server and when you will send the request to server using a browser it show below result

2. Outputting HTML file
  1. var http = require('http');
  2. var fs = require('fs');
  3. http.createServer(function (req, res) {
  4.     res.writeHead(200, {'Content-Type': 'text/html'});
  5.     var html = fs.readFileSync(__dirname + '/index.html');
  6.    
  7.     res.end(html);
  8. }).listen(8080,'localhost');
The above code reads the index.html file and produces and HTML output.
index.html
  1. <html>
  2. <head>
  3.     <title>
  4.     Hello Node
  5. </title>
  6.     <body>
  7.         <h1>
  8.             Hello Sarthak!
  9.         </h1>
  10.     </body>
  11. </head>
  12. </html>
Output when you send a request to localhost:8080

3. Outputting Dynamic HTML Content
  1. var http = require('http');
  2. var fs = require('fs');
  3. http.createServer(function (req, res) {
  4.     res.writeHead(200, {'Content-Type': 'text/html'});
  5.     var html = fs.readFileSync(__dirname + '/index2.html','utf8');
  6.     var msg = "Hello Sarthak, this is dynamic content!!!";
  7.    html = html.replace('{message}', msg);
  8.     res.end(html);
  9. }).listen(8080,'localhost');
index.html
  1. <html>
  2. <head>
  3.     <title>
  4.     Hello Node
  5. </title>
  6.     <body>
  7.         <h1>
  8.             {message}
  9.         </h1>
  10.     </body>
  11. </head>
  12. </html>

4. Outputting JSON
  1. var http = require('http');
  2. http.createServer(function (req, res) {
  3.     res.writeHead(200, {'Content-Type': 'application/json'});
  4.   
  5.     var obj = {
  6.         firstname: 'Sarthak',
  7.         lastname: 'Srivastava'
  8.     };
  9.     res.end(JSON.stringify(obj));
  10. }).listen(8080,'localhost');
MIME type is changed to ‘application/json

5. Routing 
Means when we hit different URLs we should get Navigated to different pages.
  1. var http = require('http');
  2. var fs = require('fs');
  3. http.createServer(function (req, res) {
  4. if(req.url === '/'){
  5.     res.writeHead(200, {'Content-Type': 'text/plain'});
  6.     res.end('Hello World How are you !');
  7. }
  8. else if(req.url === '/more'){
  9. res.writeHead(200, {'Content-Type': 'application/json'});
  10.   
  11.     var obj = {
  12.         firstname: 'Sarthak',
  13.         lastname: 'Srivastava'
  14.     };
  15.     res.end(JSON.stringify(obj));
  16. }
  17.    
  18. else{
  19.     res.writeHead(404);
  20.     res.end();
  21. }
  22. }).listen(8080,'localhost');
when you hit localhost:8080
it displays this

but when you hit localhost:8080/more
it displays this

else for wrong URL it will show blank or 404.

Comments

Popular posts from this blog

AWS LEX - Developing a Simple Chat Bot

Connecting Mongo DB with NodeJS

BUILDING A CHAT BOT USING MICROSOFT AZURE BOT SERVICE & LUIS