我正在研究聚合物应用程序,我想要从简单的rest API中包含一些数据,但由于CORS,我认为将其封装在一个简单的服务器端快速路由中。
每当我尝试获取新路线时,它会加载聚合物定义的404页面,在控制台中我可以看到我被发送到/ src / latestValues,所以我认为这里的聚合物工作正在拦截我的请求,也许?
如何向我的应用程序添加快速路由,我可以使用node-rest-client将一些外部数据提取到我的页面中?
/*eslint-env node*/
//------------------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------------------
// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
// create a new express server
var app = express();
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/build/es6-bundled'));
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// REST client for latest values:
var Client = require('node-rest-client').Client;
var client = new Client();
client.registerMethod("xxx", "xxx", "GET");
// Return a page
app.get('/latestValues', function(req, res){
client.methods.getVanValues(function (data, response) {
var readings2 = [
data
]
res.send(readings2);
});
});
// Render index.html on the main page, specify the root
app.get('/', function(req, res){
res.sendFile("index.html", {root: '.'});
});
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
答案 0 :(得分:0)
我认为我解决了这个问题,出于我目前所知的原因,在Chrome中,如果我尝试在相同的主机名中访问不同的路径,它需要一个应用程序,它会将/ src /添加到路径中并找不到任何内容,路由工作正常通过休息客户端所以我认为我的问题已经解决,我可以开始在我的应用程序中使用该数据,虽然我也对app.js做了一些更改,所以我在这里包括它的完整性:
/*eslint-env node*/
//------------------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------------------
// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
// create a new express server
var app = express();
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/build/es6-bundled'));
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// Return a page
app.get('/latestValues', function(req, res){
// REST client for latest values:
var Client = require('node-rest-client').Client;
var client = new Client();
client.registerMethod("xxx", "xxx", "GET");
client.methods.getVanValues(function (data, response) {
var readings2 = [
data
]
res.status(200).send(readings2);
});
});
// Render index.html on the main page, specify the root
app.get('*', function(req, res){
res.sendFile("index.html", {root: './'});
});
// start server on the specified port and binding host
app.listen(appEnv.port, '0.0.0.0', function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});