node.js - retrieving json properties from databse using express -
i new express. have created api crud app in express access cloudant db.
now want parse properties json files have on db , put them properties.
i have tried " request ( url, function(error, response, data) { body = data; } didn't work. body undefined.
var express = require('express'); var http = require('http'); var app = express(); var bodyparser = require('body-parser'); var request = require("request"); var body; var cloudant = require('cloudant'); app.set('port', process.env.port || 3000); app.set('json spaces', 2); app.use(bodyparser.json()); app.use(bodyparser.urlencoded({ extended: true })); //==== configure database ======================================// var services = json.parse(process.env.vcap_services || "{}"); var cloudantcreds = {}; (var servicename in services) { if (servicename.indexof("cloudantnosqldb") > -1) { cloudantcreds = services[servicename][0]['credentials']; } } var services = {cloudant:{}}; if (json.stringify(cloudantcreds) == "{}") { services = require("./services.json"); } var dbconfig = { account : cloudantcreds.username || services.cloudant.username || "", password : cloudantcreds.password || services.cloudant.password || "", dbname : services.cloudant.dbname || "device_info" }; var cloudant = cloudant(dbconfig); var db = cloudant.db.use(dbconfig.dbname); app.set('cloudant', cloudant); app.set('db', db); var db = require('./lib/cloudant/db.js'); //==============================================================// //==== create student database =================================// // curl <url>/cloudant/createdb // //==============================================================// app.all(db.paths.createdb, db.createdb); //==== create / insert document ================================// // curl <url>/cloudant/set // //==============================================================// app.all(db.paths.create, db.create); //==== list documents in database ==========================// // curl <url>/cloudant/list // //==============================================================// app.all(db.paths.list, db.list); //==== read 1 document based on id =============================// // curl <url>/cloudant/get?id=3 // //==============================================================// app.all(db.paths.read, db.read); //==== update 1 document based on id ===========================// // curl <url>/cloudant/update?id=3&firstname=bob&lastname=peter // //==============================================================// app.all(db.paths.update, db.update); //==== delete 1 document based on id ===========================// // curl <url>/cloudant/delete?id=3 // //==============================================================// app.all(db.paths.delete_, db.delete_); //==== delete / drop student database===========================// // curl <url>/cloudant/deletedb // //==============================================================// app.all(db.paths.deletedb, db.deletedb); request("http://localhost:3000/cloudant/get?_id=215729fc5e5785b", function(error, response, data) { body = data; }); http.createserver(app).listen(app.get('port'), function(){ console.log('express server listening on port ' + app.get('port')); console.log(body); });
Comments
Post a Comment