npm init
npm install express --save
npm install mongodb --save
npm install mongoose --save
npm install cors --save
npm install nodemone --save
npm install body-parser --save
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ProductSchema = new Schema({
title: String,
price: Number,
instock: Boolean,
photo: String,
});
module.exports = mongoose.model('Product', ProductSchema);
var express = require('express');
var bodyParser = require('body-parser');
var cors = require('cors');
var app = express();
var mongoose = require('mongoose');
var product = require('./product');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8090;
var router = express.Router();
app.use(cors());
app.use('/api', router);
app.listen(port);
console.log('REST API is runnning at ' + port);
mongoose.connect('mongodb://localhost:27017/ganesha', { useNewUrlParser: true });
router.use(function (req, res, next) {
// do logging
// do authentication
console.log('Logging of request will be done here');
next(); // make sure we go to the next routes and don't stop here
});
router.route('/products').post(function (req, res) {
var p = new product();
p.title = req.body.title;
p.price = req.body.price;
p.instock = req.body.instock;
p.photo = req.body.photo;
p.save(function (err) {
if (err) {
res.send(err);
}
res.send({ message: 'Product Created !' })
})
});
router.route('/products').get(function (req, res) {
product.find(function (err, products) {
if (err) {
res.send(err);
}
res.send(products);
});
});
router.route('/products/:product_id').get(function (req, res) {
product.findById(req.params.product_id, function (err, prod) {
if (err)
res.send(err);
res.json(prod);
});
});
router.route('/products/:product_id').put(function (req, res) {
product.findById(req.params.product_id, function (err, prod) {
if (err) {
res.send(err);
}
prod.title = req.body.title;
prod.price = req.body.price;
prod.instock = req.body.instock;
prod.photo = req.body.photo;
prod.save(function (err) {
if (err)
res.send(err);
res.json({ message: 'Product updated!' });
});
});
});
router.route('/products/:product_id').delete(function (req, res) {
product.remove({ _id: req.params.product_id }, function (err, prod) {
if (err) {
res.send(err);
}
res.json({ message: 'Successfully deleted' });
})
});
<html>
<head><title>s</title>
<script
src="https://code.jquery.com/jquery-3.3.1.js"
integrity="sha256-2Kok7MbOyxpgUVvAk/HJ2jigOSYS2auK4Pfzbm7uH60="
crossorigin="anonymous"></script>
<script>
$(document).ready(function () {
//Ajax Start
$(document).ajaxStart(function () {
$("#div1").css("display", "block");
});
$(document).ajaxStop(function () {
$("#div1").css("display", "none");
})
$(document).ajaxComplete(function () {
$("#div1").css("display", "none");
});
//Ajax Complete
$("#btn").click(function () {
$("table tr").remove();
$("table").append("<tr><th>id</th><th>title</th><th>price</th><th>instock</th></tr>");
$.getJSON("http://localhost:8090/api/products/", function (data) {
$.each(data, function (i, value) {
$("table").append("<tr id=rows><td>" + value._id + "</td><td>" + value.title + "</td><td>" + value.price + "</td><td>" + value.instock + "</td></tr>");
})
})
})
$("#btn1").click(function () {
var myKeyVals = { title: document.getElementById("title").value , price:document.getElementById("price").value , instock :document.getElementById("instock").value }
$.ajax({
url: 'http://localhost:8090/api/products/',
dataType: 'json',
type: 'post',
contentType: 'application/json',
data: JSON.stringify( myKeyVals ),
processData: false,
success: function( data, textStatus, jQxhr ){
$('#response pre').html( JSON.stringify( data ) );
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
})
$("#btn2").click(function () {
var myKeyVals = {_id: document.getElementById("id1").value , title: document.getElementById("title").value , price:document.getElementById("price").value , instock :document.getElementById("instock").value }
$.ajax({
url: 'http://localhost:8090/api/products/'+ document.getElementById("id1").value ,
dataType: 'json',
type: 'put',
contentType: 'application/json',
data: JSON.stringify( myKeyVals ),
processData: false,
success: function( data, textStatus, jQxhr ){
$('#response pre').html( JSON.stringify( data ) );
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
})
$("#btn3").click(function () {
var myKeyVals = {_id: document.getElementById("id1").value }
$.ajax({
url: 'http://localhost:8090/api/products/'+ document.getElementById("id1").value ,
dataType: 'json',
type: 'delete',
contentType: 'application/json',
data: JSON.stringify( myKeyVals ),
processData: false,
success: function( data, textStatus, jQxhr ){
$('#response pre').html( JSON.stringify( data ) );
},
error: function( jqXhr, textStatus, errorThrown ){
console.log( errorThrown );
}
});
})
})
</script>
</head>
<body>
<div>
<input type="button" name="name" id="btn" value="Get Employee" />
<hr />
<table border="1" width="300px">
</table>
<div id="div1" style="display:none">
<img src="../images/ajax_linq_MVC.png" /></div>
</div>
<hr>
<div id="div2">
Id <input type="text" name="id1" id="id1" /><br/>
Title <input type="text" name="title" id="title" /><br/>
Price <input type="text" name="price" id="price" /><br/>
InStock <input type="text" name="instock" id="instock" /><br/>
<input type="button" name="name2" id="btn1" value="Insert Employee" />
<input type="button" name="name3" id="btn2" value="Update Employee" />
<input type="button" name="name4" id="btn3" value="Delete Employee" />
</div>
<div id="response">
<pre></pre>
</div>
</body>
</html>