@@ -311,6 +311,268 @@ console.log(`Addition of number 10 and 12 : ${add.Addition(10, 12)}`);
311311console.log(` Subtration of number 14 and 12 : ${sub.subtration(14, 12)}` );
312312```
313313
314+ ## Day 7 - HttpModules
315+
316+ 1 . Node js are providing some of the inbuild module which we help to devlop Node apllication.<br />
317+ 2 . Http module is one of inbuild module which will help to create web server for web application.<br />
318+ 3 . It will help to see the output in browser<br /><br />
319+
320+ 4 . Importing the http module <br />
321+ ``` ruby
322+ const http = require (' http' )
323+ ```
324+
325+ 5 . Now yoou need to create the server, with the input paramter of function of req and res. <br />
326+ ``` ruby
327+ // create the server which will take input of req and output as rs
328+ http.createServer(function(req, res){
329+
330+ ```
331+ 6 . At the end you can mention on which port it will listen.
332+
333+ ``` ruby
334+ }).listen(4000 , () => console.log(" Your application is running on Port 4000" ));
335+ ```
336+
337+ 7 . At the end you can see the full code where we can write it like.
338+ ``` ruby
339+ // Importing the http module
340+ const http = require (' http' )
341+
342+ // create the server which will take input of req and output as rs
343+ http.createServer(function(req, res){
344+
345+
346+ // Adding requst header
347+ res.writeHead(200 , {' Content-Type' : ' text/html' });
348+
349+ // Pass query string in URL localhost: 4000 / 123
350+ var qs = req.url;
351+
352+ res.write(" <h1>hello Satya<h1>" + qs);
353+
354+ // you need to complete the response here.
355+ res.end();
356+
357+ }).listen(4000 , () => console.log(" Your application is running on Port 4000" ));
358+ ```
359+ 8 . you can stop server using ctrl + c
360+
361+ ### Nodemon
362+
363+ 1 . In above code if you are changing anything in code you to stop server and rerun again.
364+ 2 . To avoid thatyou need to install nodemon.
365+ 3 Nodemon is a utility that will monitor for any changes in your source and automatically restart your server
366+ 4 . Just use nodemon instead of node to run your code, and now your process will automatically restart when your code changes.
367+ 5 . Installing nodemon
368+ > npm install -g nodemon <br />
369+
370+ run the node application <br />
371+
372+ > nodemon index.js <br />
373+
374+ <b >*** If above will not work follow the below process</b > <br />
375+
376+ First,Install Nodemon as ``` npm install --save nodemon ``` <br />
377+
378+ Than in package.json write the followings <br />
379+
380+ ``` ruby
381+ " scripts" : {
382+ " server" : " nodemon server.js"
383+ },
384+ ```
385+ Now in terimnal check for.<br />
386+
387+ > npm run server <br />
388+ > npm run server Index.js
389+
390+
391+ ## Day 8 - Filesystem
392+ 1 . File system is use to read and print the content of file.
393+ 2 . The Node File System (fs) module can be imported using the following syntax
394+
395+ ``` ruby
396+ var fs = require (" fs" )
397+ ```
398+ 3 . You can read it using the arrow function like below.
399+
400+ ``` ruby
401+ fs.readFile(__dirname + " \\ dummyfile.txt" ," utf-8" ,(err, data) =>
402+ {
403+ if (err) throw err;
404+ console.log(data);
405+ });
406+ ```
407+ 4 . You can write the data to the file
408+ ``` ruby
409+ fs.writeFile(' dummyfile.txt' , ' Learn Node js in step by step' , function(err) {
410+ if (err) {
411+ return console.error(err);
412+ }
413+ });
414+ ```
415+
416+ ### Read HTML File
417+ You can use the same process to read the HTML files also.
418+
419+ You need to import both fs - file sysem and HTTP to create the server.
420+ ``` ruby
421+ const fs = require (" fs" );
422+ const http = require (" http" );
423+ ```
424+
425+ Now you need to create the server to see that file into browser and read that file using the fs as like below.
426+
427+ ``` ruby
428+ const server = http.createServer((req, res) => {
429+
430+ fs.readFile(" myfirstpage.html" ," utf-8" , (err, data) =>
431+ {
432+ if (err) throw err;
433+ res.writeHead(200 , {" content-type" :"text/html" });
434+ res.write(data)
435+ res.end();
436+ })
437+
438+
439+ }).listen(" 4000" , console.log(" Website is running on 4000 port." ));
440+ ```
441+ ### Read file synchronously
442+
443+ 1 . The way we were reading the file is asnyc.<br />
444+ 2 . NodeJs provide both the way to read the file, async and sync. <br />
445+
446+ > Async Method <br />
447+ Here out will first print : File Read Successfully Than content of file.
448+
449+ ``` ruby
450+ const fs = require (" fs" );
451+
452+ fs.readFile(" dummyfile.txt" ," utf-8" ,(err, data) =>
453+ {
454+ if (err) throw err;
455+ console.log(data);
456+ });
457+
458+ console.log(" ++++++++++++++" );
459+ console.log(" File read successfully" );
460+ ```
461+
462+ > Sync method. <br />
463+ a. In Async method we have call back but in Sync method we dont have call back. <br />
464+ b. Better to keep it in try catch to catch the exception.<br />
465+ c. Here first file data will print and then "File read successfully". <br />
466+
467+ ``` ruby
468+ const fs = require (" fs" );
469+
470+ try
471+ {
472+ var data = fs.readFileSync(" dummyfile.txt" ," utf-8" )
473+ console.log(data);
474+ }
475+ catch (e)
476+ {
477+ throw e;
478+ }
479+
480+ console.log(" ++++++++++++++" );
481+ console.log(" File read successfully" );
482+ ```
483+
484+ ## Day 9.0 - Events
485+ ###No Update...
486+
487+ ## Day 9.1 : Nodejs connection to SQL
488+
489+ In this session i am going to demonstrate how we can connect to database.
490+
491+ 1 . I am taking the example of how we can connect to SQL database.
492+ 2 . First you need to take the refrence of MSDatabase.
493+ 3 . For more details you will get on below link. <br />
494+ https://www.npmjs.com/package/node-mssql - it is old ways<br />
495+ 4 . Or by using mssql, For more details you can see below link.<br />
496+ https://www.npmjs.com/package/mssql <br />
497+ Install mssql package like below
498+ ``` ruby
499+ npm install mssql
500+ ```
501+
502+ 5 . You need to enable sql Network services from services.msc. <br />
503+
504+ 6 . Enable tcp/ip protocol as mentioned in below link of your system. <br />
505+ https://stackoverflow.com/questions/25577248/node-js-mssql-tedius-connectionerror-failed-to-connect-to-localhost1433-conn
506+
507+ 7 . Restart your system to see the effect of changes. <br />
508+ 8 . Import MSSQL to application. <br />
509+ ``` ruby
510+ var sql = require (' mssql' );
511+ ```
512+ 9 . Create the configuration for DB connection.
513+ ``` ruby
514+ const config = {
515+ user: ' sa' ,
516+ password: ' password1' ,
517+ server: ' REETPC' , // You can use ' localhost\\ instance' to connect to named instance
518+ database: ' test12' ,
519+ }
520+ ```
521+
522+ 10 . Inserting records to the database
523+ ``` ruby
524+ sql.connect(config).then(pool => {
525+
526+ return pool.request()
527+ .input(' CustName' , sql.VarChar (50 ), " satya" )
528+ .input(' Phonenumber' , sql.VarChar (10 ), " 45677889" )
529+ .input(' Age' , sql.Int , 23 )
530+ // You can either insert records by hard code query
531+ // .query(" INSERT INTO [test12].[dbo].[Customer] ([CustName] ,[Phonenumber] ,[Age]) VALUES( @CustName, @Phonenumber, @Age)" )
532+
533+ // OR you can write a store procedure and call that
534+ .execute(' InsertCustomer' ,)
535+ // .output(' output_parameter' , sql.VarChar (50 ))
536+ }).then(result => {
537+ console.dir(result)
538+ }).catch (err => {
539+ console.log(err);
540+ })
541+
542+ ```
543+
544+ 11 . Here is the way you can use to get the records using hard coded query
545+ ``` ruby
546+ sql.connect(config).then(() => {
547+ return sql.query` select * from [dbo].[Customer]`
548+ }).then(result => {
549+ console.log(result)
550+ }).catch (err => {
551+ console.log(err);
552+ })
553+ ```
554+
555+ 12 . Fetch the result using store procedure.
556+ ``` ruby
557+ sql.connect(config).then(pool => {
558+ return pool.request().execute(" GetAllCustomer" )
559+ }).then(result => {
560+ console.log(result)
561+ }).catch (err => {
562+ console.log(err);
563+ })
564+ ```
565+
566+ 13 . Fetch the selected result using store procedure.
567+ ``` ruby
568+ sql.connect(config).then(pool => {
569+ return pool.request().input(' Customerid' , sql.Int , 2 ).execute(" GetCustomerbyId" )
570+ }).then(result => {
571+ console.log(result)
572+ }).catch (err => {
573+ console.log(err);
574+ })
575+ ```
314576
315577## Day 10 - ExpressJs
316578
0 commit comments