简体   繁体   English

Node.js和Restful API服务

[英]nodejs and restful api service

I have created a angularjs application without nodejs usage. 我创建了一个没有使用nodejs的angularjs应用程序。 I am using "chrome-add server extension" to run the app locally and using Bluemix cloud to deploy the files and run the app. 我正在使用“ chrome-add服务器扩展”在本地运行该应用程序,并使用Bluemix Cloud部署文件并运行该应用程序。 I have created a java service in Bluemix and http.get to get the service to my front end. 我已经在Bluemix和http.get中创建了一个Java服务,以将该服务带到我的前端。 But I am facing an issue with CORS in the front end sometime. 但是我有时会在前端遇到CORS的问题。 So I took a suggestion to create a nodejs file to get the service and integrate to angular controller instead of http get method. 因此,我建议创建一个nodejs文件来获取服务并将其集成到角度控制器而不是http get方法。 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. 我的问题是我不想在nodejs中创建服务器配置,而只是在nodejs(app.get)中获得服务并传递给angularjs控制器。 The app is already invoked by the Chrome extension port. Chrome扩展程序端口已调用该应用程序。

The code i have written in nodejs server file is, 我在nodejs服务器文件中编写的代码是,

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. 我的问题是,是否可以在不创建Web服务器的情况下使用nodejs,而仅将其余响应传递给angular模块。 As i am new to node and angular, is there any example to do this? 由于我是node和angular的新手,有没有这样做的例子?

You cannot do your CORS handler URL that way. 您不能以这种方式创建CORS处理程序URL。 You will need to have a local path which proxies to the mybluemix URL, using something like https://github.com/request/request . 您将需要使用诸如https://github.com/request/request之类的本地路径来代理mybluemix URL。

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. 然后,Angular代码需要使用/getDetails URL,而不是完整的mybluemix URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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