Node Express路由在本地工作但不在远程服务器上工作

时间:2017-05-15 10:49:44

标签: node.js express digital-ocean

我有一个本地工作的节点应用程序,并使用Express进行路由。我试图使用their guide在数字海洋上进行设置。当我导航到/时,我可以看到我的应用程序的主页,但我无法访问任何内页。

要调试我停止了通过pm2运行的应用程序,只需通过调用节点app.js'来运行它。在终端中,以便我可以看到控制台日志消息。

我对快速路线进行了评论测试:

app.get('/', function (req, res) {
console.log('/ route requested');
if (req.isAuthenticated()) {
  res.render('home',
    {
      user: req.user
    });
} else {
  res.render('home',
    {
      user: null
    });
}
});

app.get('/signup', function (req, res) {
    console.log('/signup route requested');
    res.render('signup');
});

当我要求' /'打印控制台日志消息。但是试图访问' /注册'终端中没有显示任何内容。我被带到一个默认的404页面(nginx / 1.10.0(Ubuntu)。

我尝试将Nginx设置为detailed here的反向代理。这是我的nginx配置来自etc / nginx / sites-available / default

server {
        listen 80 default_server;
        listen [::]:80 default_server;

        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
        #
        # Note: You should disable gzip for SSL traffic.
        # See: https://bugs.debian.org/773332
        #
        # Read up on ssl_ciphers to ensure a secure configuration.
        # See: https://bugs.debian.org/765782
        #
        # Self signed certs generated by the ssl-cert package
        # Don't use them in a production server!
        #
        # include snippets/snakeoil.conf;

        root /var/www/html;

        # Add index.php to the list if you are using PHP
        index index.html index.htm index.nginx-debian.html;

        server_name _;

        location / {
proxy_pass http://localhost:2000;
        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;
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
        #
        #location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #

索引页面也不会从我的公共目录加载资源,例如样式。

我很感激任何帮助尝试在远程服务器上加载Express路由和资源。如果有更多信息可以帮助调试,请告诉我。

2 个答案:

答案 0 :(得分:1)

根据您发布的nginx配置判断,似乎您在try_files旁边使用了proxy_pass指令,这可能会导致竞争条件。 nginx可能会尝试将您的URI作为文件提供,这几乎绝对不能。

Location指令中删除以下行应该可以解决问题:

# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;

答案 1 :(得分:0)

由于您从 nginx 收到默认的404页面,这可能意味着您的node.js应用程序无法直接访问,但在某些nginx安装后面。真的吗?因为在这种情况下,你可能必须正确设置nginx,而express与它无关。

也许您应该尝试直接连接到nodejs应用程序进行验证(在大多数示例中,这是[host]:3000)。