AngularJS、Nodejs 和 Nginx 应用程序在尝试访问端点时返回 404

标签 angularjs node.js nginx

所以,我尝试在我的 AngularJS 客户端和 NodeJS 服务器之间放置一个 Nginx 反向代理。我的所有静态文件均按预期提供,但使用 Angular 服务中的 $http.get 访问的某些路由返回 404。(例如 app.get('/sessionInit') 或 app.get('/login'))。

我将在其中附加我的代码的一些相关部分,希望能够以任何方式帮助您。

这是我的 API 端点:

app.get('sessionInit', function(req, res){
    if(sess){
        res.send(JSON.stringify({
            code : 610,
            scope : "session",
            message : "Session on the way !",
            session : sess
        }));
    }else {
        res.send(JSON.stringify({
            code : 613,
            scope : "session",
            message : "Session does not exists or has expired please log in again.",
            session : null
        }));
    }
});

这是我的 Angular 要求:

    this.getSession = function getSession(){
                var promise = $http({
                    url: '/sessionInit',
                    method: "GET"
                }).then(function (result) {
                    if(result.data.code == 610){
                        //update browser cookies
                        $cookies.putObject("userSession", result.data.session);
                        setUserInfo();
                        if(typeof($cookies.get("userSession")) != null) return $cookies.get("userSession");
                        else return null;
                    }else{
                        console.log(result.data.code, result.data.message, result.data.session);
                        return null;
                    }
                });

                return promise;
            };

这是我的 nginx 配置文件:

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html/law-bid-nodejs;
    index index.php index.html index.htm;

    # Make site accessible from http://localhost/
    server_name *mypublicip*;

    location / {
        try_files $uri $uri/ =404;
        proxy_set_header 'Access-Control-Allow-Origin' '$http_origin';
        proxy_set_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE';
        proxy_set_header 'Access-Control-Allow-Headers' 'X-Requested-With,Accept,Content-Type, Origin';
        proxy_redirect off;         
        proxy_set_header X-Real-IP  $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $http_host;
        proxy_pass         http://127.0.0.1:3000;
    }

    error_page 404 /404.html;

    # redirect server error pages to the static page /50x.html
    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
    #   # With php5-fpm:
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
    }

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    location ~ /\.ht {
        deny all;
    }
}

当尝试从我有路线的 NodeJS 服务器请求带有 Angular 的“sessionInit”时,它返回 publicip/sessionInit 404 not find 错误。 任何帮助表示赞赏。谢谢!

最佳答案

sessionInit之前放置一个/

app.get('/sessionInit', function(req, res){
    if(sess){
        res.send(JSON.stringify({
            code : 610,
            scope : "session",
            message : "Session on the way !",
            session : sess
        }));
    }else {
        res.send(JSON.stringify({
            code : 613,
            scope : "session",
            message : "Session does not exists or has expired please log in again.",
            session : null
        }));
    }
});

在您的 Nginx 配置中 try_files $uri $uri/=404; 也会导致问题。 /sessionInit 不是现有文件,因此将重定向到 404,在 location/{.. 部分之前为您的 api 添加特定部分,并从您的 Angular 应用程序调用 /api/sessionInit:

location /api/ {
    proxy_pass  http://nodejs/;
    proxy_redirect off;
    proxy_set_header Host $host ;
    proxy_set_header X-Real-IP $remote_addr ;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ;
    proxy_set_header X-Forwarded-Proto https;
    proxy_connect_timeout 30;
    proxy_send_timeout 300;
    proxy_read_timeout 900;
    send_timeout 30;
}

server_name 中,不要输入 IP,而是输入主机名,例如:mywebsite.local,然后编辑 hosts 文件(在 linux/max 上为 /etc/hosts)并附加 127.0.0.1 mywebsite.local

--

与问题无关

不要为 res.send(JSON.stringify(..)) 烦恼,而使用 res.json(..) ,它看起来更像以下内容:

app.get('/sessionInit', function(req, res){
    if(sess){
        res.json({
            code : 610,
            scope : "session",
            message : "Session on the way !",
            session : sess
        });
    }else {
        res.json({
            code : 613,
            scope : "session",
            message : "Session does not exists or has expired please log in again.",
            session : null
        });
    }
});

也要小心 CORS并考虑使用expressjs/cors如果需要的话。

关于AngularJS、Nodejs 和 Nginx 应用程序在尝试访问端点时返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37707624/

相关文章:

javascript - 将属性应用于 Angular Directive(指令)中的自定义嵌套元素

javascript - 使用 connect-route 在路由器中传递可选参数

javascript - 如何制作不同版本的JS代码(开发版和生产版)?

nginx-ingress 为特定路径添加自定义 header

javascript - 是否可以在 AngularJS 中模拟按键?

angularjs - 处理 save() 方法 $ngResource 上的错误回调

angularjs - 使用 Bootstrap 3 和 Angular 的条件工具提示

javascript - 如何使用未知数量代币的 promise ?

node.js - PassportJS 在创建用户后挂起

ssl - 让我们加密与 cloudflare 或两者兼而有之?