NodeJS CRUD Rest API for application

NodeJS CRUD Rest API for application
Spread the love

NodeJS CRUD Rest API for application

NodeJS CRUD Rest API for application is most important things, as we discussed previous blog that how to setup NodeJS and installation. Now are talking that how to create REST API for web development and apps. Before stat read this blog please read previous blog then you can batter understand that how to create rest api in NodeJS.

You could pass an ID of 1 this: http://localhost:5000/1 and this should return the row with the ID of 1. I hope this makes sense. It’s a basic SELECT statement.

Get Data by ID
// Get an beer
app.get(‘/:id’, (req, res) => {
pool.getConnection((err, connection) => {
if(err) throw err
connection.query(‘SELECT * FROM table_name WHERE id = ?’, [req.params.id], (err, rows) => {
connection.release() // return the connection to pool
if (!err) {
res.send(rows)
} else {
console.log(err)
}

console.log(‘The data from beer table are: \n’, rows)
})
})

});

when will you implement that code to your project then you will get output and we can test this api using postman which i will already dicussed in my previous blog.

So Now we will understand that how to delete record api in node js please find below example.

App.js Delete a record
// Delete a record
app.delete(‘/:id’, (req, res) => {

pool.getConnection((err, connection) => {
if(err) throw err
connection.query(‘DELETE FROM table_name WHERE id = ?’, [req.params.id], (err, rows) => {
connection.release() // return the connection to pool
if (!err) {
res.send(`table with the record ID ${[req.params.id]} has been removed.`)
} else {
console.log(err)
}

console.log(‘The data from beer table are: \n’, rows)
})
})

});

Open your postman then you can test api

Add Record to database table please find below code

// Add record in table
app.post(”, (req, res) => {

pool.getConnection((err, connection) => {
if(err) throw err

const params = req.body
connection.query(‘INSERT INTO table_name SET ?’, params, (err, rows) => {
connection.release() // return the connection to pool
if (!err) {
res.send(`table_name with the record ID has been added.`)
} else {
console.log(err)
}

console.log(‘The data from table_name table are:11 \n’, rows)

})
})

});

App.js Update a record in table
// Update a record / table_name

app.put(”, (req, res) => {

pool.getConnection((err, connection) => {
if(err) throw err
console.log(`connected as id ${connection.threadId}`)

const { id, name, tagline, description, image } = req.body

connection.query(‘UPDATE table_name SET name = ?, tagline = ?, description = ?, image = ? WHERE id = ?’, [name, tagline, description, image, id] , (err, rows) => {
connection.release() // return the connection to pool

if(!err) {
res.send(`table_name with the name: ${name} has been added.`)
} else {
console.log(err)
}

})

console.log(req.body)
})

})

Now You should now be able to read, write, update and delete data.

App.js Full view

const express = require(‘express’)
const bodyParser = require(‘body-parser’)
const mysql = require(‘mysql’)

const app = express()
const port = process.env.PORT || 5000;

// parse application/x-www-form-urlencoded

app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json

app.use(bodyParser.json())

// MySQL

const pool = mysql.createPool({
connectionLimit : 20,
host : ‘localhost’,
user : ‘root’,
password : ‘your_password’,
database : ‘nodejs_jp’
})

// Get all from table

app.get(”, (req, res) => {
pool.getConnection((err, connection) => {
if(err) throw err
console.log(‘connected as id ‘ + connection.threadId)
connection.query(‘SELECT * from table’, (err, rows) => {
connection.release() // return the connection to pool

if (!err) {
res.send(rows)
} else {
console.log(err)
}

// if(err) throw err
console.log(‘The data from table are: \n’, rows)
})
})

})

// Get an table

app.get(‘/:id’, (req, res) => {
pool.getConnection((err, connection) => {
if(err) throw err
connection.query(‘SELECT * FROM table WHERE id = ?’, [req.params.id], (err, rows) => {
connection.release() // return the connection to pool
if (!err) {
res.send(rows)
} else {
console.log(err)
}

console.log(‘The data from table are: \n’, rows)
})
})

});

// Delete a table record

app.delete(‘/:id’, (req, res) => {

pool.getConnection((err, connection) => {
if(err) throw err
connection.query(‘DELETE FROM table WHERE id = ?’, [req.params.id], (err, rows) => {
connection.release() // return the connection to pool
if (!err) {
res.send(`table with the record ID ${[req.params.id]} has been removed.`)
} else {
console.log(err)
}

console.log(‘The data from table are: \n’, rows)
})
})

});

// Add record in table

app.post(”, (req, res) => {

pool.getConnection((err, connection) => {
if(err) throw err

const params = req.body
connection.query(‘INSERT INTO table SET ?’, params, (err, rows) => {
connection.release() // return the connection to pool
if (!err) {
res.send(`table with the record ID has been added.`)
} else {
console.log(err)
}

console.log(‘The data from table are:11 \n’, rows)

})
})

});

app.put(”, (req, res) => {

pool.getConnection((err, connection) => {
if(err) throw err
console.log(`connected as id ${connection.threadId}`)

const { id, name, tagline, description, image } = req.body

connection.query(‘UPDATE table SET name = ?, tagline = ?, description = ?, image = ? WHERE id = ?’, [name, tagline, description, image, id] , (err, rows) => {
connection.release() // return the connection to pool

if(!err) {
res.send(`table with the name: ${name} has been added.`)
} else {
console.log(err)
}

})

console.log(req.body)
})

})

// Listen on enviroment port or 5000

app.listen(port, () => console.log(Listening on port ${port}))

Run this full code to your server thanks to stay here and read blog.

Leave a Reply

Your email address will not be published. Required fields are marked *