I have http requests in the front-end for CORS, but I can’t change that server headers to support CORS.
const url = "http://121.42.154.150:8081/chart/h?cf=AVERAGE&end=1481009206&graph_type=h&id=24022&start=1480922806&sum=off";
fetch(url).then(res => {
console.log('res', res);
})
Is there any way to fix this without changing server side?
My approach
I find a way for workaround:
- make Meteor as a REST API server;
- make a http request to Meteor server from Meteor client;
- forward the query from the client to the target API server and return the results back to the Meteor client.
But I get stuck to build a REST API service on the Meteor server.
1. using fetch()
to the same origin (Meteor server) will always return a success
// client side
fetch('/hello/world').then(res => {
console.log('res', res);
})
I didn’t implement any server side code to response /hello/world
, but it will get the response with status 200
.
2. try to use express on the meteor
I tries to build the API service with express by implementing meteor-express:
// server side
var app = express();
WebApp.connectHandlers.use(Meteor.bindEnvironment(app));
app.get('/hello/world', function (req, res) {
res.send('Hello World')
});
It still doesn’t work. I can’t get the data . The fetch()
in the client side will always get the same results no matter what route I query.
3. try using Restivus to build a REST server on the Meteor
Negative. I failed to install restivus by meteor add nimble:restivus
. It shows:
=> Errors while adding packages:
While loading package npm-bcrypt@0.7.8_2:
error: Command failed: /Users/Xaree/.meteor/packages/meteor-tool/.1.4.2_3.1fwg30s++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/bin/npm rebuild
--update-binary
gyp ERR! configure error
gyp ERR! stack Error: read ETIMEDOUT
gyp ERR! stack at exports._errnoException (util.js:911:11)
gyp ERR! stack at TLSWrap.onread (net.js:558:26)
gyp ERR! System Darwin 16.1.0
...
I have no idea how to achieve my goal now.
Please give me some advices (or sample code that would work). I’m really stuck.