jef
jef

Reputation: 4083

HTTP POST: express server returns 405 error if it enables serve-index

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.

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); 

environment

Upvotes: 0

Views: 2348

Answers (2)

Roman
Roman

Reputation: 151

I know this is kinda old, but I faced the same problem and there wasn't much about it on the internet, so I want to share how I worked around this without using a prefix like suggested:

// The folder you want to serve
var contentFolder = __dirname + '/public'

app.use(express.static(contentFolder))

app.use('/', (req, res, next) => {
    if (req.method !== 'GET' && req.method !== 'HEAD') {
        return next()
    }

    serveIndex(contentFolder, { icons: true })(req, res, next)
});

// No 405 at this point anymore
app.post('/post', function (req, res) {
...
})

With inspiration from https://github.com/webpack/webpack-dev-server/blob/89ffb86cd26425c59e3937ca06a2c804a01b8f1d/lib/Server.js

Upvotes: 1

robertklep
robertklep

Reputation: 203419

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).

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'));

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).

Upvotes: 2

Related Questions