// getAll endpoint server.get("/products",function(req,res,next) { db.products.find(function(err,products){ res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' }); res.end(JSON.stringify(products)); }); return next(); });
server.post('/product', function(req, res, next) { var product = req.params; db.products.save(product, function(err, data) { res.writeHead('200', { 'Content-Type': 'application/json; charset=utf-8' }); res.end(JSON.stringify(data)); }); return next(); });
server.get("/", function (req, res, next) { res.writeHead(200, {'Content-Type': 'application/json; charset=utf-8'}); res.end(JSON.stringify(app)); return next(); });
server.del('/product/:id', function (req, res, next) { db.products.remove({ id: req.params.id }, function (err, data) { res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' }); res.end(JSON.stringify(true)); }); return next(); });
next() }) res.writeHead(500) })
server.put('/product/:id', function(req,res,next){ //get the existing product db.products.findOne({id:req.params.id},function(err,data){ var updateProduct = {}; for(var n in data) { updateProduct[n] = data[n]; } for(var n in req.params){ updateProduct[n] = req.params[n]; } db.products.update({id:req.params.id},updateProduct,{multi:false},function(err,data){ res.writeHead(200, { 'Content-Type':'application/json; charset=utf-8' }); res.end(JSON.stringify(data)); }); }); return next(); });
server.get('/product/:id', function (req, res, next) { db.products.findOne({ id: req.params.id }, function (err, data) { res.writeHead(200, { 'Content-Type': 'application/json; charset=utf-8' }); res.end(JSON.stringify(data)); }); return next(); });