Javascript 如何使用Meteor JS Node server代理第三方ajax请求
我有一个不支持CORS的第三方JSON端点,我的应用程序应该通过服务器代理请求。我今天已经研究了几个小时,没有看到一个简单的解决方案(几个复杂的…) 所以基本上我需要做一些事情,比如Javascript 如何使用Meteor JS Node server代理第三方ajax请求,javascript,meteor,http-proxy,Javascript,Meteor,Http Proxy,我有一个不支持CORS的第三方JSON端点,我的应用程序应该通过服务器代理请求。我今天已经研究了几个小时,没有看到一个简单的解决方案(几个复杂的…) 所以基本上我需要做一些事情,比如request('http://localhost:3000/publications/jsonProxy),它调用Meteor服务器。然后,我需要一个出版物,该出版物使用安全令牌从第三方请求数据,并且我需要将该数据返回到浏览器 我尝试过这样的事情: const request = require('request'
request('http://localhost:3000/publications/jsonProxy)
,它调用Meteor服务器。然后,我需要一个出版物,该出版物使用安全令牌从第三方请求数据,并且我需要将该数据返回到浏览器
我尝试过这样的事情:
const request = require('request');
if (Meteor.isServer) {
Meteor.publish('jsonProxy', function jsonProxyPublication() {
var options = {
url: 'https://somewhere.com/api/endpoint',
headers: {
'API-Key': '123'
}
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
let info = JSON.parse(body);
console.log(info);
return info
} else {
console.error( error, response )
}
}
request(options, callback);
return this.ready()
});
}
然后:curl localhost:3000/publications/jsonProxy
。这可能不是正确的方法,我有点迷路了
看起来很简单,有人能告诉我将这些数据返回到浏览器的正确方法吗?看起来我让它工作了。下面是示例代码,而不是“真实”代码,因为我必须从上下文中提取它 /server/proxy/json api.js
import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http'
Meteor.methods( {
'jsonProxy' () {
const apiUrl = 'https://api.com/api'
const response = HTTP.get( apiUrl, {
headers: {
'API-Key': '123'
}
} ).data
console.log( `${ apiUrl } response:`, response )
return response
}
} )
import './proxy/jsonodds.js'
Meteor.call( 'jsonProxy', ( error, result ) => {
if( !error ) {
Session.set( 'jsonData', result )
} else {
Session.set( 'jsonData', `Error: ${ JSON.stringify( error ) } `)
}
} )
Template.app.helpers( {
jsonData() {
return Session.get( 'jsonData' )
}
} )
/server/main.js
import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http'
Meteor.methods( {
'jsonProxy' () {
const apiUrl = 'https://api.com/api'
const response = HTTP.get( apiUrl, {
headers: {
'API-Key': '123'
}
} ).data
console.log( `${ apiUrl } response:`, response )
return response
}
} )
import './proxy/jsonodds.js'
Meteor.call( 'jsonProxy', ( error, result ) => {
if( !error ) {
Session.set( 'jsonData', result )
} else {
Session.set( 'jsonData', `Error: ${ JSON.stringify( error ) } `)
}
} )
Template.app.helpers( {
jsonData() {
return Session.get( 'jsonData' )
}
} )
/imports/ui/pages/app/app.js
import { Meteor } from 'meteor/meteor';
import { HTTP } from 'meteor/http'
Meteor.methods( {
'jsonProxy' () {
const apiUrl = 'https://api.com/api'
const response = HTTP.get( apiUrl, {
headers: {
'API-Key': '123'
}
} ).data
console.log( `${ apiUrl } response:`, response )
return response
}
} )
import './proxy/jsonodds.js'
Meteor.call( 'jsonProxy', ( error, result ) => {
if( !error ) {
Session.set( 'jsonData', result )
} else {
Session.set( 'jsonData', `Error: ${ JSON.stringify( error ) } `)
}
} )
Template.app.helpers( {
jsonData() {
return Session.get( 'jsonData' )
}
} )
/imports/ui/pages/app/app.html
<template name="app">
<div id="app">
{{#each jsonData}}
{{> itemTemplate}}
{{/each}}
</div>
</template>
<template name="itemTemplate">
<p>{{displayName}}</p>
</template>
{{{#每个jsonData}
{{>itemTemplate}
{{/每个}}
{{displayName}}
编辑:我不确定这是否重要代理位于
服务器文件夹中,但它正在工作,我有更多的东西要构建。更新-我找到了答案,不久将发布使用WebApp
模块定义服务器路由的文章,这会更干净。