JAVASCRIPT 19
Web Technologies - Lecture 2 Guest on 28th February 2022 09:14:22 AM
  1. NodeJS scenario is module based.
  2.  
  3. basic-server.js content:
  4. // NOT PART of EXAM, ONLY FOR KNOWLEDGE
  5.  
  6. const http = require('http');
  7. const router = (req, res)=>{
  8.     console.log('${req.method} ${req.url}')
  9.     res.writeHead(200, {'Content-Type':'text/html'});  // 200 means OK response, the type of response is text/html  
  10.     res.end('<h1>Hello World</h1>');
  11. }
  12.  
  13. const routes = {
  14.     'GET':{
  15.         '/':(req,res)=>{
  16.        
  17.             res.writeHead(200, {'Content-Type':'text/html'});  // 200 means OK response, the type of response is text/html  
  18.            // res.end('<h1>Hello World</h1>');
  19.            res.end(`<form method ="post">
  20.            <input type="text" name="username" placeholder="username">
  21.            <br>
  22.            <input type="password" name="password" placeholder="password">
  23.            <br>
  24.            <input type="submit" value="Login">
  25.        </form>`);
  26.  
  27.    
  28.     },
  29.     'about':(req,res)=>{
  30.         res.writeHead(200, {'Content-Type':'text/html'});  // 200 means OK response, the type of response is text/html  
  31.         res.end('<h1>About Us</h1>');  
  32.     }
  33. },
  34.     'POST':{        '/':(req,res)=>{
  35.         let body = '';
  36.         req.on('data', (data)=>
  37.         {
  38.             body+=data;
  39.         });
  40.         req.on
  41.  
  42.         }
  43.     },
  44.     '':()=>{}
  45.  
  46. }
  47.  
  48. // For Each is just for iteration, does not return any value
  49.  
  50. const router = (req, res)=>{
  51.     console.log('${req.method} ${req.url}')
  52.     let resolveRoute = routes[req.method][req.url]
  53.     if (resolveRoute !== undefined){
  54.         resolveRoute(req,res);
  55.     } else{
  56.         routes['NA'](req, res)
  57.     }
  58. }
  59.  
  60. end of content
  61.  
  62. http
  63. .createServer((req, res)=>{
  64.     res.writeHead(200, {'Content-Type':'text/html'});  // 200 means OK response, the type of response is text/html  
  65.     res.end('<h1>Hello World</h1>');
  66. })
  67. .listen(3000, ()=> console.log('Server is running on http://localhost:3000')); // running on 3000 port
  68.  
  69. // NodeMon is used to sync the code, this will sync any changes whenever the code is changed so it does not require any kind of restart.
  70. // GET and POST
  71. // GET will receive
  72. // POST will send the response
  73.  
  74.  
  75.  
  76.  
  77. There are different types of modules
  78. Dependency Module
  79. Non-dependency Module (Does not do anything to the code, but provide support related to code such as NodeMon, monitors the code and syncs instantly
  80.  
  81. creating a module first
  82.  
  83. package.json file is created automatically
  84.  
  85. index.js - a module file
  86. content:
  87. constant express = require('express');
  88. const app = express();
  89. const port = 4000;
  90.  
  91. const logger = (req, res, next)=>{ // Next shows that it is a middleware, next means the next middleware to be executed.
  92. req.msg =  '/nThis message is from middleware!!!');
  93. next();
  94. }else{
  95. res.send("Access denied")
  96. }
  97. }
  98.  
  99. const auth = (req, res, next)=>{
  100. if (req.query.name === "Peter"){ // Query means GET, BODY means POST
  101. next()
  102.  
  103. }
  104. }
  105.  
  106. app.use(logger); // To use the middleware, we have to use this.
  107. app.get('/', (req, res)=>{
  108. res.send('<h3>Hello World</h3> ${msg}');
  109. })
  110.  
  111. app.get('/users', auth, (req, res)=>{
  112. res.send('<h3>Users Route</h3> ${msg}');
  113. })
  114.  
  115. app.listen(port, ()=> console.log('Server is running on http://localhost:${port});
  116.  
  117.  
  118. // There are different template engines
  119. // The controller decides the route to go.

Coding Base is for source code and general debugging text.

Login or Register to edit, delete and keep track of your pastes and more.

Raw Paste

Login or Register to edit or fork this paste. It's free.