Node / Express没有找到Angular http json帖子的路由(404) [英] Node/Express not finding route for Angular http json post (404)

查看:267
本文介绍了Node / Express没有找到Angular http json帖子的路由(404)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个新的Angular客户端,它应该与我的Node / Express服务器通信。我目前正在尝试开发第一步,即登录。这应该是一个http json帖子。事实证明,每次我从客户端到服务器执行该帖子时,Node / Express服务器都找不到路径的路径。

I'm developing a new Angular client which should communicate with my Node/Express server. I'm currently trying to develop the first step aka the login. This should be an http json post. It turns out that every single time I execute that post from the client to the server, the Node/Express server doesn't find the route for my path.

在服务器的控制台日志中,对于我的post json请求,我发现以下stacktrace:

On the console log of the server, for my post json request I find following stacktrace:

OPTIONS /api/auth/facebook/mobile 404 274.092 ms - 1980

节点/快递方:

我的路线

app.post('/api/auth/facebook/mobile', authenticationHandler.handleFacebookMobileLoginRequest);

定义了json的正文解析器:

Body parser for json is defined:

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));

在Angular客户端,我这样做:

On the Angular client side, I do:

http({
             method: 'POST',
             url: 'http://192.168.1.101:3000/api/auth/facebook/mobile',
             headers: { 'Content-Type': 'application/json; charset=UTF-8;' },
             data: {fbToken: authResponse.accessToken}
      })
        .then(function (response) {
 ...

有趣的事实:


  1. 当我从Java应用程序或Android原生应用程序调用它时,相同的路径工作正常,例如

  1. Same route works fine when I call it from a Java app or and Android native app, like

final JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, SERVER_URL + "/api/auth/facebook/mobile", params, jsonRequestListener, errorListener);


  • 在我的Angular客户端上,当我更改数据时使用 params 然后它可以工作,找到路线...但我的意思是没有任何意义。此外,我是然后在Angular客户端遇到问题,答案没有分别处理,答案永远不会落在 .then(... 函数中。这在其他客户中再次运作良好。

  • On my Angular client, when I change data with params then it works, the route is found...but I mean that doesn't make any sense. Moreover, I've got then a problem on the Angular client side, the answer isn't processed respectively the answer never land in the .then(... function. Which again works well in other clients.

    任何人都有想法,线索或我应该给Strange博士打电话?

    Anyone got an idea, a clue or should I call Dr. Strange?

    推荐答案

    可能的浏览器预检请求 CORS问题?

    Possible browser preflighted request CORS issue?

    尝试使用 CORS模块

    var cors = require('cors')
    
    app.use(cors());
    

    这篇关于Node / Express没有找到Angular http json帖子的路由(404)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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