开始使用node.js来处理rest api.Badly遇到问题,不知道如何修复它。调用http://localhost:8080/api/write/v1.0/login
时打印错误money
任何帮助将不胜感激。
App.js
TypeError: Function.prototype.apply: Arguments list has wrong type
<br> at /Volumes/Developer/Node Sample/sample/app.js:38:16
<br> at Layer.handle [as handle_request] (/Volumes/Developer/Node Sample/sample/node_modules/express/lib/router/layer.js:95:5)
<br> at next (/Volumes/Developer/Node Sample/sample/node_modules/express/lib/router/route.js:131:13)
<br> at next (/Volumes/Developer/Node Sample/sample/node_modules/express/lib/router/route.js:125:14)
<br> at next (/Volumes/Developer/Node Sample/sample/node_modules/express/lib/router/route.js:125:14)
<br> at next (/Volumes/Developer/Node Sample/sample/node_modules/express/lib/router/route.js:125:14)
<br> at next (/Volumes/Developer/Node Sample/sample/node_modules/express/lib/router/route.js:125:14)
<br> at next (/Volumes/Developer/Node Sample/sample/node_modules/express/lib/router/route.js:125:14)
<br> at next (/Volumes/Developer/Node Sample/sample/node_modules/express/lib/router/route.js:125:14)
<br> at next (/Volumes/Developer/Node Sample/sample/node_modules/express/lib/router/route.js:125:14)
Api.js
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var dbUtils = require('./utility/dbUtilties.js');
var config = require('./config/config.js');
var api = require('./api/api');
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = process.env.PORT || 8080; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
//router.get('/', function(req, res) {
// var params = req.body;
// var db = dbUtils.connect();
// if(db){
// console.log(db);
// res.json({ message: 'hooray! welcome to our api!' });
// }else{
// res.json({ message: 'connection failed' });
// }
//});
app.all('/api/write/'+config.WRITE_API_VERSION+'/:apiName', function (req, res) {
console.log("in api end1");
api.handle.apply('write' , req.method.toLowerCase(), 'req.params.apiName', req, res);
});
app.all('/api/read/'+config.READ_API_VERSION+'/:apiName', function (req, res) {
console.log("in api end2");
api.handle.apply('read' , req.method.toLowerCase(), req.params.apiName, req, res);
});
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);
答案 0 :(得分:2)
错误非常明确:
TypeError:Function.prototype.apply:参数列表的类型错误
Function.prototype.apply()
签名是fun.apply(thisArg, [argsArray])
,它期望一个对象(或null)后跟一个数组,你首先传递一个字符串。您的apply
电话应如下:
api.handle.apply(null, ['write', req.method.toLowerCase(), 'req.params.apiName', req, res]);
如果您没有任何具体的理由使用apply
(例如将thisArg
设置为特定值),您只需调用该函数:
api.handle('write', req.method.toLowerCase(), 'req.params.apiName', req, res);