我正在运行的hapi.js服务器分布在两个端口中。 在端口4000
我正在运行以下服务器
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' };
在端口5000上我正在运行以下api响应
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'};
我的问题是,如何将网址http://localhost:5000/retrieve
中的数据读取到网址http://localhost:4000
。
我曾尝试使用wreck
,但它无效
答案 0 :(得分:0)
尝试request:
Request("http://localhost:5000/retrieve", function(err, res, body){
console.log(res.body);
});