Express 405 POST not allowed

The route works fine from chrome extension POSTMAN, with Angular it doesn't work.

Ok, here is my js express code :

var express = require('express');
var router = express.Router();
var app = express();
var bodyParser = require('body-parser')
var routes = require('./routes');
var connection  = require('express-myconnection');

app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded

/** Serve our app on root path */
app.use('/', express.static('app'));

/** Login API */
app.post('/login', routes.login);

      

And here comes the Angular code :

$http({
    method: 'POST',
    url: apiUrl + 'login',
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded' // necessary for expressjs
    },
    transformRequest: function(obj) {
        var str = [];
        for (var p in obj) {
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        }
        return str.join("&");
    },
    data: user
});

      

Not sure what happened! This is what I get:

enter image description here

+3


source to share


1 answer


Try checking headers and content for both requests, so there is a difference between them. The response header Allow

does not explicitly include POST, so there might be a CORS issue.



+4


source







All Articles