HTTP POST:如果启用服务索引,快速服务器将返回 405 错误 [英] HTTP POST: express server returns 405 error if it enables serve-index

查看:59
本文介绍了HTTP POST:如果启用服务索引,快速服务器将返回 405 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Node.js express 开发 Web 应用程序.我必须实现一些处理 HTTP POST 的 Web API.实际上我可以处理 HTTP POST,但是我发现如果我启用 serve-index,下面的示例会返回 405 错误.

I'm developing a web application with Node.js express. I have to implement some web APIs which handle HTTP POST. Actually I could handle HTTP POST, but I found if I enable serve-index, below sample returns 405 error.

我下面的示例工作正常(我已经确认 RestClient 是一个 chrome 扩展).但是如果我注释掉 app.use(serveIndex.., 这个服务器通过使用 POST 到 '/profile' 返回 405 (Method Not Allowed) 错误.有谁知道会发生什么?

My below sample works correctly (I've confirmed RestClient which is a chrome extension). But if I comment out app.use(serveIndex.., this server returns 405 (Method Not Allowed) error by using POST to '/profile'. Does anyone know what happens?

 var express     = require('express'),                                                                                          
     serveIndex  = require('serve-index'),                                                                                      
     app         = express();                                                                                                   

 // app.use(serveIndex(__dirname + '/public')); // this is the reason of 405 error?                                      

 var bodyParser = require('body-parser');                                                                                       
 app.use(bodyParser.json());                                                                                                    
 app.use(bodyParser.urlencoded({ extended: true }));                                                                            

 app.post('/profile', function(req, res) {                                                                                      
   var user_id = req.body.id;                                                                                                   
   var pass = req.body.pass;                                                                                                    
   res.send('user_id:', user_id, ', pass:', pass);                                                                              
 });                                                                                                                            

 app.listen(3333); 

环境

  • node.js v5.3.0
  • express v4.13.3
  • 服务索引 v1.7.2

推荐答案

判断依据 代码serve-index 尝试处理传递给它的所有请求.如果这些请求不是 GETHEADOPTIONS,它们会被 405(或 OPTIONS 的 200)拒绝>).

Judging by the code, serve-index tries to handle all requests that are passed to it. If those requests are not GET, HEAD or OPTIONS, they are rejected with a 405 (or a 200 for OPTIONS).

根据您的具体设置,您可以将 serve-index 挂载到特定前缀上,因此只有具有该前缀的请求才会传递给它:

Depending on your exact setup, you can mount serve-index on a particular prefix, so only requests with that prefix are passed to it:

// Request to `/static/some/dir/` will be mapped to `__dirname/public/some/dir/`
app.use('/static', serveIndex(__dirname + '/public'));

或者,您可以将其作为最后一个中间件(在您的路由器之后)包含在内,尽管对于没有现有处理程序的请求,如果使用不同的 HTTP 方法(而不是404更合适).

Alternatively, you may be able to include it as the last middleware (after your routers), although for requests that don't have an existing handler it would still generate a 405 if those use a different HTTP method (instead of a more appropriate 404).

这篇关于HTTP POST:如果启用服务索引,快速服务器将返回 405 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆