Tuesday, 6 June 2017

how to consume hapi.js rest response within a route

The hapi.js server i am running are distributed into two ports. on port 4000

i am running the following server

var handlebars = require("handlebars");
var Vision =require('vision');
var Inert = require('inert');
var Wreck = require('wreck');
var mongo = require('mongoskin');
var fs = require("fs");
var db = mongo.db("mongodb://localhost:27017/connect_db", {native_parser:true});
var ObjectID = mongo.ObjectID;


// Create a server with a host and port
exports.register = function (server, options , next ) {
//register view engine
server.register(Vision);
server.register(Inert);

server.views({
    engines: {
        'html': handlebars
    },
    relativeTo :__dirname,
    path: 'views',
    isCached: false
   });


server.route({
    method: 'GET',
    path: '/',
    handler: function (request, reply) {
        db.collection("hello_collection").find().toArray(function(err, items) {
           var body = {title: items};
          reply.view('index.html', body);
          db.close();
       });
    }
});
server.route({
    method: 'GET',
    path: '/edit',
    handler: function (request, reply) {
    var params = request.query.id;
    db.collection("hello_collection").find({"_id":ObjectID(params)}).toArray(function(err, items) {
            
            reply.view('edit.html',items[0]);

                    db.close();
       });;
         
    }
});

server.route({
    method: 'GET',
    path: '/delete',
    handler: function (request, reply) {
        var params = request.query.id;
        db.collection("hello_collection").remove({ "_id": ObjectID(params)});
        db.close();
        reply().redirect("/");
    }
    
});


server.route({
    method: 'POST',
    path: '/new_data',
    handler: function (request, reply) {
    var data = request.payload;
     db.collection("hello_collection").insert( { name: data.name, description: data.description, content: data.content  } );

    db.close();
    reply().redirect("/");
       
    }
});
server.route({
    method: 'POST',
    path: '/edit_data',
    handler: function (request, reply) {
    var data = request.payload;
    db.collection('hello_collection').update({ "_id" : ObjectID(data.id) },{$set: { name : data.name, description : data.description , content : data.content } }
);
    db.close();
    reply().redirect("/");
       
    }
});

server.route({
  method: "GET",
  path: "/bower_components/{path1}/{path2}",
  handler: function (request, reply) {
    var Path = "./web/bower_components/" + request.params.path1  +"/" + request.params.path2;
    console.log(Path);
    reply.file(Path); 
  }
});


  return next();
};

exports.register.attributes = { name: 'web' };

on the port 5000 i am running the following api response

var handlebars = require("handlebars");
var mongo = require('mongoskin');
var db = mongo.db("mongodb://localhost:27017/connect_db", {native_parser:true});
var ObjectID = mongo.ObjectID;
exports.register = function (server , options , next){
        server.route({
                method: 'GET',
                path: '/retrieve',
                handler : function(request, reply) {
                    if(!request.query.id){              
                           
                        db.collection("hello_collection").find({}).toArray(function(err, items){
                                
                                reply(items);
                                db.close();
                        });
                        
                     }
                    else{
                         var params = request.query.id;
                         db.collection("hello_collection").find({"_id":ObjectID(params)}).toArray(function(err, items) {
            
                          reply(items[0]);

                    db.close();
       });
        
                    }   
         
                }
        
        });
return next();
};
exports.register.attributes = { name : 'api'};

my question is, how do i read the data from the url http://localhost:5000/retrieve to the url http://localhost:4000 . I have tried to use wreck but it is not working



via Saikat Mondal

No comments:

Post a Comment