Posts

Showing posts from 2022

Node js connect with MongoDB

 index.js: const express = require ( 'express' ); const app = express (); const mongoose = require ( 'mongoose' ); const connectDB = async () => {     mongoose . connect ( 'mongodb://localhost:27017/e-Comm' );     const productSchema = new mongoose . Schema ({});     const products = mongoose . model ( 'products' , productSchema )     const data = await products . find ();     console . warn ( data ); } connectDB (); app . listen ( 5000 );

CRUD API with MySQL

 index.js: const express = require ( 'express' ); const con = require ( './config12' ) const app = express (); app . use ( express . json ()); app . get ( "/" , ( req , resp ) => {     con . query ( "select * from user" , ( err , result ) => {         if ( err ){             resp . send ( "error" )         } else {             resp . send ( result )         }     }) }); app . post ( '/' , ( req , resp ) => {       const data = req . body ;     con . query ( 'Insert into users3 SET?' , data ,( error , result , fields ) => {         if ( error ) error ;         resp . send ( result )     }) }); app . put ( '/:id' , ( req , resp ) => {     const data =[ req . body . name , req . body . password , req . params . id ];     con . query ( "Update users SET name=?, password=? where id =? " , data , ( err , result , fields ) => {     if ( err ) throw error ;     resp . send ( result )    }) }

Node JS GET API with MySQL

 config.js const mysql = require ( 'mysql' ) const con = mysql . createConnection ({     host : 'localhost' ,     user : 'root' ,     password : '' ,     database : 'test' }); con . connect (( err ) => {     if ( err ){         console . warn ( "error in connection" );     } }) module . exports = con ; index.js const express = require ( 'express' ); const con = require ( './config12' ) const app = express (); app . get ( "/" , ( req , resp ) => {     con . query ( "select * from user" , ( err , result ) => {         if ( err ){             resp . send ( "error" )         } else {             resp . send ( result )         }     }) }); app . listen ( 5000 )

Node JS connect with mysql

 index.js: const mysql = require ( "mysql" ); const con = mysql . createConnection ({     host : 'localhost' ,     user : 'root' ,     password : '' ,     database : 'test' }); con . connect (( err ) => {     if ( err ){         console . warn ( "error" )     }     else {         console . warn ( "connected" )     } }); con . query ( "select * from user" , ( err , result ) => { console . warn ( result ) })

Search API with multiple filed - NODE JS

 config.js -  const mongoose = require ( 'mongoose' ) mongoose . connect ( 'mongodb://localhost:27017/e-Comm' ) Product.js: const mongoose = require ( 'mongoose' ); const productSchema = new mongoose . Schema ({     name : String ,     price : Number ,     brand : String ,     category : String });   module . exports = mongoose . model ( 'products' , productSchema ); index.js: const express = require ( 'express' ); require ( './config' ); const product = require ( './product' ); const app = express (); app . use ( express . json ()); app . post ( '/create' , async ( req , resp ) => {     let data = new product ( req . body );     let result = await data . save ();     console . log ( result )     resp . send ( result ); }); app . get ( '/list' , async ( req , resp ) => {     let data = await product . find ();     resp . send ( data ); }); app . delete ( '/delete/:_id' , async ( req , r

CRUD with Mongoose

 index.js -  const mongoose = require ( 'mongoose' ); const saveInDB = async () => {     await mongoose . connect ( 'mongodb://localhost:27017/e-Comm' );     const ProductSchema = new mongoose . Schema ({         name : String ,         brand : String ,         price : Number ,         category : String     });     const ProductsModel = mongoose . model ( 'products' , ProductSchema );     let data = new ProductsModel ({ name : 'm9' , brand : 'LG' , price : 150 , category : 'mobile' });     let result = await data . save ();     console . log ( result ); } const updateInDB = async () => {     await mongoose . connect ( 'mongodb://localhost:27017/e-Comm' );     const ProductSchema = new mongoose . Schema ({         name : String ,         brand : String ,         price : Number ,         category : String     });     const Product = mongoose . model ( 'products' , ProductSchema

CRUD API Mongodb

Api.js: const express = require ( 'express' ); const dbConnect = require ( './mongodb' ); const mongodb = require ( 'mongodb' ) const app = express (); app . use ( express . json ()); app . get ( '/' , async ( req , resp ) => {     let data = await dbConnect ();     data = await data . find (). toArray ();     console . log ( data )     resp . send ({ data }) }); app . post ( '/' , async ( req , resp ) => {     let data = await dbConnect ();     let result = await data . insert ( req . body )         resp . send ( result ) }); app . put ( '/' , async ( req , resp ) => {     let data = await dbConnect ();     let result = await data . updateOne (         { name : req . body . name },         { $set : req . body }     )     console . log ( req . body )     resp . send ({ result : "Updated" }) }); app . delete ( '/:id' , async ( req , resp ) => {     console . log ( req . params . id )     const

Node JS - Make HTML page

index3.js -    const express = require ( 'express' ); const path = require ( 'path' ); const app = express (); const publicPath = path . join ( __dirname , 'public' ); console . log ( publicPath ); app . use ( express . static ( publicPath )); app . listen ( 5000 ); -------------------------------------- index.html (under public folder) <!DOCTYPE html > <html lang = "en" > <head>     <title> Home Page </title> </head> <body>     <h1> Home Page Node JS </h1> </body> </html> ------------------------------------ about.html (under public folder) <!DOCTYPE html > <html lang = "en" > <head>         <title> About Us Page </title> </head> <body>     <h2> About Us Page </h2>     <p> Lorem, ipsum dolor sit amet consectetur adipisicing elit. Dolor fugit tenetur ex doloremque quis expedita dignissimos tempore officia! Volu

Express JS - Render HTML and JSON

  const express = require ( 'express' ); const app = express (); app . get ( '' , ( req , resp ) => {     // console.log("Data sent by Browser =>", req.query.name)     // resp.send('Hello, this is a home page');     resp . send ( `     <h1>Jai Sri Krishna</h1>     <a href="/about">Go to About Page</a>     ` ); }); app . get ( '/about' , ( req , resp ) => {     // resp.send('Hello, this is a about page');     resp . send ( `     [         {             name:'krishan',             email:'krishan@gmail.com'         },         {             name:'Radhe',             email:'radhe@gmail.com'         }             ]     <a href="/">Go to Home Page</a>     ` ); }); app . listen ( 5000 );

Express JS Example

  const express = require ( 'express' ); const app = express (); app . get ( '' , ( req , resp ) => {     resp . send ( 'Hello, this is a home page' ); }); app . get ( '/about' , ( req , resp ) => {     resp . send ( 'Hello, this is a about page' ); }); app . listen ( 5000 );

Node.js CRUD with File System

  const fs = require ( 'fs' ); const path = require ( 'path' ); const dirPath = path . join ( __dirname , 'crud' ); const filePath = ` ${ dirPath } /apple.txt` ; // fs.writeFileSync(filePath, 'this is a apple.txt file'); // fs.readFile(filePath,'utf8', (err, item)=>{ //     console.log(item) // }) // fs.appendFile(filePath, 'I am updating apple.txt file', (err)=>{ //     if(!err){ //         console.log("file is updated") //     } // }) // fs.rename(filePath, `${dirPath}/fruit.txt`, (err)=>{ //     if(!err){ //         console.log("File name has been renamed") //     } // }) fs . unlinkSync ( ` ${ dirPath } /fruit.txt` )

Node Js API Tutorial 2022

Image
 First Static API: -  api1.js -  const http = require ( 'http' ); const data = require ( './data' ); http . createServer (( req , resp ) => {     resp . writeHead ( 200 , { 'Content-Type' : 'application \j son' });     resp . write ( JSON . stringify ( data ));     resp . end (); }). listen ( 5000 ); -------------------------------------------- data.js : - const data =[     { name : 'krishan Dev' , email : 'krishandev.0102@gmail.com' },     { name : 'Raj Dev' , email : 'rajdev.0102@gmail.com' },     { name : 'Brijesh Dev' , email : 'brijeshdev.0102@gmail.com' },     { name : 'Bhavesh Dev' , email : 'bhaveshdev.0102@gmail.com' } ] module . exports = data ; Output:

Angular Website Template

Image
 HTML -  <div id = "header" >     <div class = "container" >         <div class = "row" >             <div class = "col-md-4" >                 <h2> Company Name </h2>             </div>             <div class = "col-sm-8" >             <ul id = "menu" class = "float-md-right" >                 <a href = "" ><li> Home </li></a>                 <a href = "" ><li> About </li></a>                 <a href = "" ><li> Services </li></a>                 <a href = "" ><li> Contact </li></a>                 <a href = "" ><li> Blog </li></a>             </ul>             </div>         </div>     </div> </div> <!-- Banner --> <div id = "banner"