Ejemplo: cómo crear un carrito de compras en el nodo js
// run this as nod.jsvar http =require('http');var url =require('url');varMongoClient=require('mongodb').MongoClient;var mongourl ="mongodb://localhost:27017/";
http.createServer(function(req, res)
res.writeHead(200,'Content-Type':'text/html');var urlQuery = url.parse(req.url,true).query;addItem(urlQuery.itemToAdd);removeItem(urlQuery.itemToRemove);
res.end();).listen(8080);functionremoveItem(remove)if(remove !==undefined)MongoClient.connect(mongourl,function(err, db)if(err)throw err;var dbo = db.db("mydb");var myquery = itemName: remove ;
dbo.collection("shoppingCart").deleteOne(myquery,function(err, obj)if(err)throw err;console.log("deleted:"+ myquery);
db.close();););functionaddItem(create)if(create !==undefined)MongoClient.connect(mongourl,function(err, db)if(err)throw err;var dbo = db.db("mydb");var myobj = itemName: create ;
dbo.collection("shoppingCart").insertOne(myobj,function(err, res)if(err)throw err;console.log("document inserted:"+ myobj);
db.close();););
Nos encantaría que puedieras compartir este escrito si lograste el éxito.
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)