node.js - 错误 : Request failed with status code 405 , 有效负载返回 HTML,而不是预期的 JSON

标签 node.js reactjs nginx amazon-ec2

我正在 AWS 上使用 Node JS + React + NGINX 设置 Web 应用程序 然后当我想访问 url/auth

它返回给我一些 HTML 代码,而不是像我想要的那样的 JSON 我在 LOCALHOST 上测试了代码,它工作正常

我尝试设置文件夹权限,因为我认为用户权限可能是问题

我还尝试在 Nginx.conf 上编辑一些内容

下面是我的 nginx 的 app.conf

upstream webapp{
    server 127.0.0.1:3018;
}

server_names_hash_bucket_size 64;
server_names_hash_max_size 512;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name 127.0.0.1;
        server_name_in_redirect off; 

    if ($http_x_forwarded_proto = 'http'){
       return 301 https://$host$request_uri;
    }


   location / {
        root /home/website/client/build;
        try_files $uri /index.html;
        log_not_found off;
        access_log off;
    }

    #error_page  405     =200 $uri;

    if ( $http_user_agent ~* (nmap|nikto|wikto|sf|sqlmap|bsqlbf|w3af|acunetix|havij|appscan) ) {
        return 403;
    }
    add_header X-Frame-Options DENY;
    add_header X-Content-Type-Options nosniff;
    add_header X-XSS-Protection "1; mode=block;";
    add_header Strict-Transport-Security "max-age=2592000; includeSubDomains" always;
}
action     {…}
​
payload: Object { isAuth: false, error: true }
​
type: "auth_member"

我期望输出代码像这样 但网络却给了我其他东西

最佳答案

您必须定义要代理的上游位置:

 # this is an example you can modify the path on as you get it
 location /api {
       proxy_pass http://webapp;
       proxy_http_version 1.1;
       proxy_set_header Upgrade $http_upgrade;
       proxy_set_header Connection 'upgrade';
       proxy_set_header Host $host;
       proxy_cache_bypass $http_upgrade;
    }


关于node.js - 错误 : Request failed with status code 405 , 有效负载返回 HTML,而不是预期的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58521997/

相关文章:

javascript - 从嵌套在函数中的函数返回数组并处理 promise 检查

Node.js express 、路由器和压缩

reactjs - 在 React 中使用 TypeScript 输入动态标签?

nginx - Docker中的NGINX服务失败,限制了内存和CPU使用率

android - 与 nginx 的 SSL 套接字连接在某些 android 和 ios 设备上不起作用

video - 使用 RTMP 的实时视频 ffmpeg 延迟

javascript - Mongoose $slice 并获取原始大小的数组

node.js - 使用nodejs在远程服务器上创建文件

css - 如何动态控制 react-bootstrap-table-next 表格列的宽度?

reactjs - 如何在 Formik 文本字段上添加清除按钮