简体   繁体   中英

nodejs and restful api service

I have created a angularjs application without nodejs usage. I am using "chrome-add server extension" to run the app locally and using Bluemix cloud to deploy the files and run the app. I have created a java service in Bluemix and http.get to get the service to my front end. But I am facing an issue with CORS in the front end sometime. So I took a suggestion to create a nodejs file to get the service and integrate to angular controller instead of http get method. My issue is that I don't want to create a server configuration in nodejs, just get the service in nodejs (app.get) and pass to angularjs controllers. The app is already invoked by the Chrome extension port.

The code i have written in nodejs server file is,

var express = require('express');

var app = express();

var express = require('express');
var app = express();

app.get('http://myblumemix-service.mybluemix.net/getDetails', function(req, res){
  //code to bind the response to angular controller
  //alert("");
  //console.log("got already running port successfully");
});

app.listen("http://127.0.0.1:8887/");

But i am not able to get the response successfully to angular controller.

My question is, could we use nodejs without creating web server and only to pass the rest response to angular modules. As i am new to node and angular, is there any example to do this?

You cannot do your CORS handler URL that way. You will need to have a local path which proxies to the mybluemix URL, using something like https://github.com/request/request .

Example:

var request = require('request');

app.get('/getDetails', function(req, res){
  request.get('http://myblumemix-service.mybluemix.net/getDetails').pipe(res);
});

Then the Angular code needs to use /getDetails URL instead of the full mybluemix one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM