"backend in nodejs" Code Answer's

You're definitely familiar with the best coding language Whatever that developers use to develop their projects and they get all their queries like "backend in nodejs" answered properly. Developers are finding an appropriate answer about backend in nodejs related to the Whatever coding language. By visiting this online portal developers get answers concerning Whatever codes question like backend in nodejs. Enter your desired code related query in the search bar and get every piece of information about Whatever code related question on backend in nodejs. 

backend in nodejs

on May 14, 2021
import React from 'react';
export default class AddBook extends React.Component {

constructor(){
        super();
        this.state = {
            bookname:'',
            author:'',

        };

    }

updateInfo = (event) =>{
        let fieldName = event.target.name;
        let fieldValue = event.target.value;
        if(fieldName === 'bookname') {
            this.setState({bookname: fieldValue});
        }
        else if(fieldName === 'author'){
            this.setState({author:fieldValue});
        }
};

addBook=(e)=>{

let {bookname,author}=this.state;
fetch('localhost:3000/add_book', {
      method: 'post',
      headers: {'Content-Type': 'application/json'},
      body: JSON.stringify({
        bookname:bookname,
        author:author,
      })
      }).then(response=>response.json()).then(data=>{
      window.alert(data)
      //Do anything else like Toast etc.
})
}

render(){
return(
<div className="add_book">

<div>
	<label>Book Name</label>
	<input onChange={this.updateInfo} name="bookname" value{this.state.bookname}/>
</div>
<div>
	<label >Author</label>
	<input onChange={this.updateInfo} name="author" value={this.state.author}/>
</div>

<button onClick={this.addBook}>Add</button>                                 
</div>
    )
}
}

Source: stackoverflow.com

Add Comment

0

backend in nodejs

on May 14, 2021
const express = require('express');
const bodyParser = require('body-parser');
var connection  = require('express-myconnection'); 
var mysql = require('mysql');

const app = express(); 
app.use(bodyParser.json());

app.use(

        connection(mysql,{

            host: 'localhost', //'localhost',
            user: 'userEHX',
            password : 'hMmx56FN4GHpMXOl',
            port : 3306, //port mysql
            database:'sampledb'

        },'pool')); //or single

       app.post('/add_book',(req,res)=>{

        let {book_name,author,} = req.body;


        if(!book_name) return res.status(400).json('Book Name cant be blank');
        if(!author) return res.status(400).json('Author cant be blank');

        var data={book_name:book_name,
                  author:author};


         var query = connection.query("INSERT INTO books set ? ",data, 
        function(err, rows)
        {

          if (err){
            //If error
              res.status(400).json('Sorry!!Unable To Add'));
              console.log("Error inserting : %s ",err );
             }
         else
          //If success
          res.status(200).json('Book Added Successfully!!')

        });


        });


         app.listen(3000, ()=> {
      console.log(`app is running on port 3000`);
});

Source: stackoverflow.com

Add Comment

0

All those coders who are working on the Whatever based application and are stuck on backend in nodejs can get a collection of related answers to their query. Programmers need to enter their query on backend in nodejs related to Whatever code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about backend in nodejs for the programmers working on Whatever code while coding their module. Coders are also allowed to rectify already present answers of backend in nodejs while working on the Whatever language code. Developers can add up suggestions if they deem fit any other answer relating to "backend in nodejs". Visit this developer's friendly online web community, CodeProZone, and get your queries like backend in nodejs resolved professionally and stay updated to the latest Whatever updates. 

Whatever answers related to "backend in nodejs"

View All Whatever queries

Whatever queries related to "backend in nodejs"

Browse Other Code Languages

CodeProZone