我刚刚开始通过API管理工具构建我的第一个API,我遇到了一些问题,我希望你能帮助我。
我的主要目标是通过创建API链接到我上传的外部Javascript文件,让我的移动应用程序和Usergrid BAAS使用此API。到目前为止,我的经验是我的代码在我尝试的第一天工作,但完全相同的代码在第二天开始破解,这非常令人困惑。
PS-我将RemoteController.js文件作为节点类型上传
这是我非常简单的代码,我希望你帮助我发现导致错误的问题
测试链接:
http://jerryhamby-test.apigee.net/vdex-baas/hello
http://jerryhamby-test.apigee.net/vdex-baas/helloworld
=============我的server-usergrid.js代码===========
var argo = require('argo');
var express = require('express');
var usergrid = require('usergrid');
var fs = require('fs');
fs.readFileSync('./RemoteController.js','utf8');
var app = express();
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(app.router);
var proxy = argo()
.target('https://api.usergrid.com')
.build();
app.get('/hello', function(req, res) {
res.send('Hello from Express2');
});
app.get('/helloworld', function(req, res) {
res.setHeader("Access-Control-Allow-Origin","*");
RemoteController.helloWorld(req, res);
});
app.all('*', proxy.run);
app.listen(3000);
=============我的RemoteController.js代码===========
function RemoteController() {
}
var client = new usergrid.client({
orgName:'myOrg',
appName:'sandbox',
logging: true, //optional - turn on logging, off by default
buildCurl: true
});
// test of helloworld
RemoteController.helloWorld = function(req, res) {
res.send('RemoteController inside helloWorld');
};
=============调用RemoteController.helloWorld时得到的错误===========
at /organizations/jerryhamby/environments/test/apis/vDex-BaaS/server-usergrid.js:35
(anonymous) at /node_modules/express/lib/router/index.js:164 (callbacks) at
/node_modules/express/lib/router/index.js:138 (param) at
/node_modules/express/lib/router/index.js:145 (pass) at
/node_modules/express/lib/router/index.js:90 (anonymous) at
/node_modules/express/lib/router/index.js:33 (router) at
/node_modules/express/node_modules/connect/lib/proto.js:193 (next) at
/organizations/grangaswamy/environments/test/apis/apigee-aws/node_modules/express/node_modules/connect/lib/middleware/multipart.js:97 (multipart) at
/organizations/grangaswamy/environments/test/apis/apigee-aws/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:64 (anonymous) at
/node_modules/express/node_modules/connect/lib/middleware/urlencoded.js:41 (urlencoded) at
/organizations/grangaswamy/environments/test/apis/apigee-aws/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:62 (anonymous) at
/node_modules/express/node_modules/connect/lib/middleware/json.js:42 (json) at
/organizations/grangaswamy/environments/test/apis/apigee-aws/node_modules/express/node_modules/connect/lib/middleware/bodyParser.js:60 (bodyParser) at
/node_modules/express/node_modules/connect/lib/proto.js:193 (next) at
/node_modules/express/node_modules/connect/lib/middleware/logger.js:158 (logger) at
/node_modules/express/node_modules/connect/lib/proto.js:193 (next) at
/node_modules/express/lib/middleware.js:30 (expressInit) at
/node_modules/express/node_modules/connect/lib/proto.js:193 (next) at
/node_modules/express/node_modules/connect/lib/middleware/query.js:45 (query) at
/node_modules/express/node_modules/connect/lib/proto.js:193 (next) at
/node_modules/express/node_modules/connect/lib/proto.js:201 (anonymous) at
/node_modules/express/node_modules/connect/lib/connect.js:65 (app) at
/Users/Apigee/src/noderunner/target/checkout/node10/node10src/src/main/javascript/io/apigee/trireme/node10/node/events.js:98 (anonymous) at
/Users/Apigee/src/noderunner/target/checkout/node10/node10src/src/main/javascript/io/apigee/trireme/node10/trireme/adaptorhttp.js:509 (anonymous) at
/Users/Apigee/src/noderunner/target/checkout/node10/node10src/src/main/javascript/io/apigee/trireme/node10/node/domain.js:183 (anonymous) at
/Users/Apigee/src/noderunner/target/checkout/node10/node10src/src/main/javascript/io/apigee/trireme/node10/node/domain.js:123 (anonymous) at
/Users/Apigee/src/noderunner/target/checkout/node10/node10src/src/main/javascript/io/apigee/trireme/node10/trireme/adaptorhttp.js:508 (anonymous) at
/Users/Apigee/src/noderunner/target/checkout/node10/node10src/src/main/javascript/io/apigee/trireme/node10/trireme/adaptorhttp.js:578 (anonymous)
答案 0 :(得分:0)
每个节点模块(文件〜=模块)都有自己的上下文。您可以从该上下文中导出变量,并将它们放入您的上下文中。
试试这个:
=============我的server-usergrid.js代码===========
var argo = require('argo');
var express = require('express');
var RemoteController = require('./RemoteController');
var app = express();
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(app.router);
var proxy = argo()
.target('https://api.usergrid.com')
.build();
app.get('/hello', function(req, res) {
res.send('Hello from Express2');
});
app.get('/helloworld', function(req, res) {
res.setHeader("Access-Control-Allow-Origin","*");
RemoteController.helloWorld(req, res);
});
app.all('*', proxy.run);
app.listen(3000);
=============我的RemoteController.js代码===========
var usergrid = require('usergrid');
function RemoteController() {
}
var client = new usergrid.client({
orgName:'myOrg',
appName:'sandbox',
logging: true, //optional - turn on logging, off by default
buildCurl: true
});
// test of helloworld
RemoteController.helloWorld = function(req, res) {
res.send('RemoteController inside helloWorld');
};
module.exports = RemoteController;