【发布时间】:2017-05-15 10:49:44
【问题描述】:
我有一个在本地运行并使用 Express 进行路由的节点应用程序。我尝试使用their guide 在数字海洋上进行设置。当我导航到 / 时,我可以看到我的应用程序的主页,但我无法访问任何内页。
为了调试,我停止了通过 pm2 运行的应用程序,并通过在终端中调用“node app.js”简单地运行它,以便我可以看到控制台日志消息。
我把 cmets 放在 Express 路由上进行测试:
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 的反向代理。这是我来自 etc/nginx/sites-available/default 的 nginx 配置
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 路由和资源加载到远程服务器上,我将不胜感激。如果他们有更多有助于调试的信息,请告诉我。
【问题讨论】:
-
这部分看起来很可疑:
# First attempt to serve request as file, then -
好点@freshnode。它不在数字海洋示例中,所以我不太确定我从哪里得到的:$ 我会尝试不使用并报告
-
@freshnode 删除配置文件的
# First attempt to serve request as file, then部分就可以了。谢谢。想把它写成答案,这样我就可以将其标记为正确? -
[编辑] 参考这个 StackOverflow 答案stackoverflow.com/a/49964108/4270123 这个解决了我的问题。
标签: node.js express digital-ocean