gpt4 book ai didi

node.js - Node 快车: 404 on some routes but not others

转载 作者:太空宇宙 更新时间:2023-11-03 23:36:51 27 4
gpt4 key购买 nike

所以我遇到了一个问题,我的一些 express.js (v3.2.0) 路由返回 404 状态代码,而其他路由则没有。我不明白为什么有些路线工作正常,而其他路线则无法解决。任何帮助将不胜感激。

服务器.js:

//npm packages
var express = require('express')
, app = express()
, server = require('http').createServer(app)
;
if(process.env.NODE_ENV == 'production') require('newrelic');

//local resources
init = require('./lib/init');
ctrl = require('./lib/ctrl')

//external async resources (global variables)
init.setup(function(){

//configure express
_util.configExpress(__dirname, app, express);

// create a new AI classifier
//
// Arguments
// ---------
// modelType - (path param) ID of the classifier
// modelParams - (body) JSON containing to configure the model
//
// Returns
// -------
// 200 - ID of created classifier
// 400 - Error during creation
app.get('/clf/create/:modelType', _util.auth(express.basicAuth), function(req, res) {
var modelParams = req.body ? req.body : {};
var modelType = req.params.modelType;
ctrl.clfCreate(modelType, modelParams, function(err, result) {
if(err) {
// at this point, assume bad request
res.status(400);
res.json({"err": err.message});
} else {
// if success, return classifier ID
res.json({"id": result});
}
res.end();
});
});

// [Unrelated endpoints...]

//handle resource update data from io server
app.all('/:clientId/:projectId/train', _util.auth(express.basicAuth), function (req, res){
ctrl.trainer(req.params.clientId, req.params.projectId, function(err, result){/*res.json(result);*/});
res.end();
});

// [Unrelated endpoints...]

//listen on app port
var port;
var expr = _env.name == 'prod' ? port = process.env.PORT : port = _env.app.ai.port;
server.listen(port);
console.log('Listening to: '+port);
});

_util.configExpress:

self.configExpress = function(dirname, app, express){                                                                                                                                                                                        
app.configure(function() {
//setup jade
app.set('views', dirname+'/views')
app.set('view engine', 'jade')
//setup friendly console logging
app.use(express.logger('dev'))
//use body parser for handling http post
app.use(express.bodyParser());
//define static file server
app.use(express.static(dirname+'/'+_env.assets));
});
}

但是当我测试端点时:

$ curl -vv --user bst:bst localhost:3002/clf/create/bnn
* Trying ::1...
* Connected to localhost (::1) port 3002 (#0)
* Server auth using Basic with user 'bst'
> GET /clf/create/bnn HTTP/1.1
> Host: localhost:3002
> Authorization: Basic YnN0OmJzdA==
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 404 Not Found
< X-Powered-By: Express
< Content-Type: text/plain
< Content-Length: 9
< Date: Thu, 02 Jul 2015 18:54:32 GMT
< Connection: keep-alive
<
* Connection #0 to host localhost left intact
Not Found%

$ -vv --user bst:bst localhost:3002/bst/drone/train
* Trying ::1...
* Connected to localhost (::1) port 3002 (#0)
* Server auth using Basic with user 'bst'
> GET /bst/drone/train HTTP/1.1
> Host: localhost:3002
> Authorization: Basic YnN0OmJzdA==
> User-Agent: curl/7.43.0
> Accept: */*
>
< HTTP/1.1 200 OK
< X-Powered-By: Express
< Date: Thu, 02 Jul 2015 18:54:45 GMT
< Connection: keep-alive
< Transfer-Encoding: chunked
<
* Connection #0 to host localhost left intact
➜ reporting git:(feature/ai-train-api) ✗

最佳答案

您正在使用 Express 版本 3,您正在尝试像版本 4 一样使用路由参数。

req.params 是一个数组,而不是版本 3 中的对象

尝试查找here .

代码示例

尝试一下,我想因为 var modelType 未定义,因此您的 ctrl,clfCreate() 函数无法发送响应。在另一个端点中,您的 res.end() 超出了 ctrl 函数的范围

app.get('/clf/create/:modelType', _util.auth(express.basicAuth), function(req, res) {
var modelParams = req.body ? req.body : {};
var modelType = req.params[0];
ctrl.clfCreate(modelType, modelParams, function(err, result) {
if(err) {
// at this point, assume bad request
res.status(400);
res.json({"err": err.message});
} else {
// if success, return classifier ID
res.json({"id": result});
}
res.end();
});
});

关于node.js - Node 快车: 404 on some routes but not others,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31192604/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com