編輯1:這裏是a mini code我做的是重現錯誤。請按照README.md
進行安裝。處理沒有片段的平均值堆棧應用程序地址'#'
編輯2:最後,我找到了一個解決方案。除了$locationProvider.html5Mode(true);
($locationProvider.hashPrefix('')
對我來說不是必要的)和<base href="/" />
,我需要在routes/index.js
而不是app.js
中添加以下內容。然後,我們不需要再添加任何更多app.js
或nginx或Apache,如this thread提及。
var express = require('express');
var router = express.Router();
var path = require('path');
... ...
router.get('*', function(req, res) {
res.sendfile('./views/index.html'); // load our public/index.html sendFile
// res.sendFile('index.html', { root: path.join(__dirname, 'views') }); // does not work
});
的一個問題是,在服務器控制檯,它給express deprecated res.sendfile: Use res.sendFile instead routes/index.js:461:9
。但res.sendFile('index.html', { root: path.join(__dirname, 'views') });
不能幫助,它返回404錯誤。
我的快遞版本是~4.14.0
......有誰知道如何解決這個問題?
OP:
我的Mac開發與Apache,可以通過 https://localhost:3000/#/home
請求均值堆棧的應用程序。在使用NGINX服務器進行生產時,應用程序可通過 https://www.myapp.io/#/home
請求。由於angular ui-router
,所有情況下都需要片段標識符#
。
所以我想讓漂亮的URL,而不#
(例如,https://www.myapp.io/home
,https://localhost:3000/home
)工作。我也做了以下情況:
在
app.config(['$stateProvider'...
添加$locationProvider.html5Mode(true); $locationProvider.hashPrefix('')
。在
index.html
在瀏覽器欄添加<base href="/" />
其結果是,自動https://localhost:3000/#/home
改變https://localhost:3000/home
,類似地對於https://www.myapp.io/#/home
。
然而,在瀏覽器將引發一個錯誤直接進入https://localhost:3000/home
或https://www.myapp.io/home
(我不知道如何在error.ejs
把以前<h1><%= message %></h1><h2><%= error.status %></h2><pre><%= error.stack %></pre>
到error.html
,所以我沒有更多細節)。
所以現在,我們的目標是讓https://localhost:3000/home
和https://www.myapp.io/home
工作。
通過以下this thread,我添加了如下到app.js
:
app.use('/js', express.static(__dirname + '/js'));
app.use('/dist', express.static(__dirname + '/../dist'));
app.use('/css', express.static(__dirname + '/css'));
app.use('/partials', express.static(__dirname + '/partials'));
app.all('/*', function(req, res, next) {
res.sendFile('index.html', { root: __dirname });
});
而且在蘋果的Apache的,這裏是我的httpd-vhosts.conf
,重新啓動apache
, https://localhost:3000/home
仍然會返回一個錯誤之後。
<VirtualHost *:443>
ServerName localhost
DocumentRoot "/Users/SoftTimur"
SSLEngine on
SSLCipherSuite ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP:+eNULL
SSLCertificateFile /etc/apache2/ssl/localhost.crt
SSLCertificateKeyFile /etc/apache2/ssl/localhost.key
<Directory "/Users/SoftTimur">
RewriteEngine on
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule^- [L]
# Rewrite everything else to index.html to allow html5 state links
RewriteRule^index.html [L]
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
在生產中,這裏是NGINX服務器模塊。重新啓動NGINX後,https://www.myapp.io/home
仍然會返回錯誤。
server {
listen 443 ssl;
server_name myapp.io www.myapp.io;
ssl_certificate /etc/letsencrypt/live/myapp.io/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/myapp.io/privkey.pem;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
ssl_ciphers 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:EC$
ssl_session_timeout 1d;
ssl_stapling on;
ssl_stapling_verify on;
add_header Strict-Transport-Security max-age=15768000;
index index.html;
root /opt/myapp;
location/{
try_files $uri $uri/ /index.html;
}
location ~ /.well-known {
allow all;
}
location/{
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Accept-Encoding "";
proxy_set_header Proxy "";
proxy_pass https://127.0.0.1:3000;
# These three lines added as per https://github.com/socketio/socket.io/issues/1942 to remove sock$
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}
任何人都可以幫忙嗎?
不,它並不能幫助。我已經做到了這一點,它的工作原理,但我的問題是關於如何處理沒有'#'的網址。 – SoftTimur
對不起,只是仔細閱讀。你的問題有很多「分心」。但我應該讀得更好。所以你的問題實際上是:爲什麼一旦部署後它不工作?因爲我無法真正查明你的確切問題...... – stevenvanc
我的問題是:給定'https:// localhost:3000 /#/ home'和'https://www.myapp.io/#/ home'工作,如何使'https:// localhost:3000/home'和'https:// www.myapp.io/home'工作? – SoftTimur