POST method is not permitted on Expressjs 405

While the route functions perfectly in POSTMAN's chrome extension, it doesn't seem to work with Angular.

Here is my Express js 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 is 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
});

I'm not sure what's going wrong! This is the result I'm getting:

Answer №1

When analyzing the headers and content of each request, it's likely you'll find discrepancies. The absence of POST in the Allow header of your response could indicate a CORS problem.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Transmit a communication from the web interface to the C++ backend system

Welcome to my web-based crypto trading platform system. The front-end of my platform is implemented in Php/NodeJS, allowing users to easily place stock orders through a simple http form request. On the backend side, I have utilized C++ to send API calls ...

Is there a way to dynamically assign column names during the import process?

In my current scenario, I am importing data from various sources into my JSON backend. The mapping file will resemble the sample below. The question is how can I efficiently utilize this mapping during data import into my backend. The idea is to provide t ...

node.js experiencing crashing following loop iteration

I'm currently developing a performance testing tool using node.js to automate the process and store the results in MySQL. This tool is designed to track the load time of specific web pages in a browser, with the measurement displayed in seconds. I am ...

What is the best way to conceal association table outcomes in a linked query using sequelize?

When connecting two tables X and Y with a table Z, the associations can be set up as follows: X.belongsToMany(Y, {through: Z}) Y.belongsToMany(X, {through: Z}) The issue arises when querying the data: X.getYs(); Y.findOne({ ... include;[{model:X ...

Leverage the JSON Web Token module within a Chrome extension

Currently in the process of developing a chrome extension but encountering an issue with loading the json web token node module in my Node.js setup. background-script.ts import jwt from 'jsonwebtoken'; // import * as jwt from '../node_mod ...

Managing multiple instances of express in a node.js application

After coming into possession of a Node.js codebase, I found myself grappling with the following snippets: var app1 = express(); app1.listen(8080) var app2 = express(); app2.listen(8081) var app3 = express(); app3.listen(8082) All of these lines reside ...

I'm having trouble persisting my mongoose model data in my MongoDB database

Hey there, I'm new to this and currently attempting to save the Amadeus object I've created into mongosh using the .save() method. However, after connecting to my file through node, editing the Amadeus object, and running amadeus.save(), I check ...

When coding in JavaScript, the value of "this" becomes undefined within a class function

I'm facing an issue with my TypeScript class that contains all my Express page functions. When I try to access the class member variable using this, I get an 'undefined' error: class CPages { private Version: string; constructor(ver ...

Using Angular's $post method to communicate with PHP CodeIgniter for making requests and handling responses

I am having trouble sending data from Angular to Codeigniter using $post. Here is the JavaScript code I am using: $scope.user.first_name = 'first name'; $scope.user.last_name = 'last name'; $http({ method: 'POST', ...

How is the Router module utilized within Express framework?

Apologies if this is a basic inquiry, but despite my efforts to research and consult documentation, I am still unable to grasp the precise function of the Route module and its intended purpose. Can someone provide clarification on this topic? ...

What is the best way to convert this jQuery code into an AngularJS implementation?

I'm diving into the world of AngularJS and looking for a more elegant solution using AngularJS principles Controller $scope.filter = function($event, active, id) { var html = ""; if(active){ $http({method: 'GET& ...

Ways to retrieve information from a URL using the .get() method in a secure HTTPS connection

As I work on handling user input from a form using node.js, express, and bodyParser, I encounter an issue. Even after using console.log(req.body), the output is {}. This is puzzling as there is data in the URL when the form is submitted successfully at htt ...

Switch the paper tab to a dropdown menu in Polymer.js

Can someone assist me in transforming the paper tab into a paper drop down menu in polymer JS? I want the drop-down to appear with a list of values when hovering over the Top menu. Activity Execution <paper-tab cla ...

Execute code once gulp task has been completed for all files

I've been experimenting with Gulp to compare its speed with Grunt, and I'm quite impressed with the results. However, there's one thing I'm unable to figure out how to do in Gulp. Specifically, I have this gulp task for minifying HTML: ...

Cannot complete request: CORS issue encountered in AngularJS

I recently started using AngularJS and I'm trying to integrate the Paytm wallet into my web application. However, when I attempt to send a request to the Paytm server from my AngularJS function, I encounter the following error: Cross-Origin Request ...

Gain entry to Zurb Foundation for Apps modules within your AngularJS application

Currently, I am developing an AngularJS application utilizing Foundation for Apps. One key element in my layout is a Foundation Apps panel that serves as the top menu. <div zf-panel="" id="topMenu" position="top" class="panel-fixed">...</div> ...

Solving the Cross-Origin Resource Sharing problem in AngularJS

While using the http dependency in AngularJS and setting headers for CORS, I am encountering an error. Please check the console.log for more information on the error. The error message reads: "XMLHttpRequest cannot load . Response to preflight request doe ...

Retrieving ng-repeat object in Angular

How can I retrieve the current object from an ng-repeat on ng-click without using $index? The $index method is giving me the wrong index due to my use of orderBy. Ideally, I would like to be able to click on the object (thumbnail) and have $scope.activePer ...

angular: the world of guarantees and assistance

I'm struggling to grasp the concept of promises. I understand what they are supposed to do, but when it comes to writing or debugging them, I hit a roadblock. MyController.js (function() { angular.module('WizmoApp').controller('St ...

Repeating the identical request in a node express application

I am using Express to forward requests to a different API server that requires OAuth 2 access tokens for protection. Whenever the token expires, the API server responds with a 401 error code. I have implemented logic in my router middleware to handle this ...