Connect NodeJS with MySQL
Program Info
Program Code
×

Connect NodeJS with MySQL

We are doing connect nodejs with mysql.

Footer

×

Connect NodeJS with MySQL

const mysql = require('mysql');

const con = mysql.createConnection({
    host:'localhost',
    user:"root",
    password:"",
    database:"node"
});

con.connect((err)=>{
   if(err)
   {
    console.log("Error")
   }else{
    console.log("Connected")
   }
})

con.query("select * from users",(err,result)=>{
    console.warn("result",result)
})

Footer

Connect NodeJS with MongoDB Database
Program Info
Program Code
×

Connect NodeJS with MongoDB Database

We connecting nodejs with mongodb database.

Footer

×

Connect NodeJS with MongoDB Database

const {MongoClient} = require('mongodb');
const url = 'mongodb://localhost:27017';
const database = 'ecommerce'
const client = new MongoClient(url);

async function dbConnect() {
    let result = await client.connect();
    let db = result.db(database);
    return db.collection('products');
    // let response = await collection.find({Name:'smart redmi Phone'}).toArray();
    // console.log(response);
    
}

// dbConnect().then((response)=>{
//     response.find().toArray().then((data)=>{
//         console.warn(data)
//     })
// })

const main = async ()=>{
    let data = await dbConnect();
    data = await data.find().toArray();
    console.warn(data)
}

main();

Footer

Create a New File
Program Info
Program Code
×

Create a New File

We creating a new file.

Footer

×

Create a New File

var fs = require('fs');

//create a file named mynewfile3.txt:
fs.writeFile('mynewfile3.txt', 'Hello content!', function (err) {
  if (err) throw err;
  console.log('Saved!');
});

              

Footer

Create Basic API
Program Info
Program Code
×

Create Basic API

We creating basic api.

Footer

×

Create Basic API

var http = require('http');

http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'application\json'});
    res.write(JSON.stringify({name:'krishna',email:'krishna@gmail.com'}));
    res.end();
}).listen(8000);

Footer

Create MySQL API
Program Info
Program Code
×

Create MySQL API

We creating mysql api.

Footer

×

Create MySQL API

const express = require('express');
const con = require('./config_mysql');
const app = express();

app.use(express.json());

app.get('/',(req,res)=>{
    con.query("select * from users",(err,result)=>{
        if(err){
            res.send("error")
        }else{
            res.send(result)
        }
    })
});

app.post('/',(req,res)=>{
    const data = req.body;
    con.query("INSERT INTO users set?",data,(error,results,fields)=>{
        if(error) error;
        res.send(results)
    })
})

app.put('/:id',(req,res)=>{
    const data = [req.body.name,req.body.email,req.body.password,req.params.id];
    con.query("UPDATE users set name = ?, email = ?, password = ? where id = ?",data,(error,results,fields)=>{
        if(error) throw error;
        res.send(results)
    })
})

app.delete('/:id',(req,res)=>{
    con.query("DELETE FROM users WHERE id =" + req.params.id,(error,results)=>{
        if(error) throw error;
        res.send(results)
    })
})

app.listen(5000);

Footer

Create Server
Program Info
Program Code
×

Create Server

We creating a server.

Footer

×

Create Server

var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World!');
}).listen(8080);

Footer


×

Name : Krishna Verma    |    Father's Name : Mr. Satish Kumar    |    Email ID : krishnaverma28081997@gmail.com    |    Religion : Hindu    |    Address : Dayampur, kanker khera, Meerut Cantt (250001), UP    |    Date of Birth : 28 August 1997    |    Marital Status : Non-Married    |    Place of Birth : Dayampur Kanker Khera Meerut Cantt.    |    Gender : Male    |    Age : 28    |    Contact : 9520335394    |    Father's Occupation : Sports man    |    Mother's Name : Smt. Kuntesh Devi    |    Mother's Occupation : Homemaker    |    Number of Brothers : 1    |    Number of Sisters : 1    |    Current Residence : Dayampur kanker khera meerut cantt    |    Family Type : Nuclear Family    |    Family Name : verma    |    Height : 5' 6''    |    Weight : 70 kg    |    Complexion : Fair    |    Body Type : Athletic    |    Hair Color : Black    |    Health Status : Good    |    Any Physical Disability : No    |    Highest Qualification : B.Tech (CSE)    |    Master Skill : PHP Laravel    |    Total Projects : 40 +    |    Total Skills : 25 +    |    Languages Known : Hindi and English    |    Website : krishnaportfolio.in    |    Siblings : 3    |    Nationality : Indian    |    Total Experience : 5 Years    |    LinkedIn : https://www.linkedin.com/in/krishna-verma-840b71356/    |    Total Programs : 10000    |    Total Projects : 40    |    Total Skills : 25    |    Total Experience : 5    |    Total DSA Problems : 165    |    Total Served Companies : 2