2016-04-13 61 views
1

快速的問題: 如何發送數據到GET請求使用鐵服務器端路由?發送數據在流星HTTP GET請求

Router.route("/api/test", function() { 
    this.response.writeHead(200, { 
    'Access-Control-Allow-Origin': '*' 
    }); 
    this.response.statusCode = 200; 
    this.response.data = {test: 'test'}; 
    this.response.end('end'); 
}, {where: 'server'}); 

回答

0

見2例下發送MeteorChef網站的響應部分,

Router.route("users/:name/profile", function() { 
    var name = this.params.name, 
     query = this.request.query, 
     fields = {}; 

    fields[ query.field ] = query.field; 

    var getUser = Meteor.users.findOne({ "profile.username": name }, { fields: fields }); 

    if (getUser) { 
     this.response.statusCode = 200; 
     this.response.end(getUser.profile); 
    } else { 
     this.response.statusCode = 404; 
     this.response.end({ status: "404", message: "User not found." }); 
    } 
}, { where: "server" }); 

所以你可以使用this.response.end像這樣把你的數據,

Router.route("/api/test", function() { 
    this.response.writeHead(200, { 
    'Access-Control-Allow-Origin': '*' 
    }); 
    this.response.statusCode = 200; 
    //this.response.data = {test: 'test'}; 
    this.response.end({ test: 'test' }); 
}, {where: 'server'}); 

我從來沒有嘗試過服務器端路由自己,所以我不確定它是否有效。