![](/img/trans.png)
[英]how to run angular 4 app and nodejs api on same port 4200 for both production and development?
[英]how to run angular 6 app with nodejs api on production?
嗨,我是angular6的新手
在開發模式下,我在操作中使用了角度4200端口和其他端口(8080)中的節點。 但現在我想將我的應用程序移至生產模式。 為此,我已經使用ng build
命令構建了我的角度項目。 我得到一個dist文件夾后,在根文件夾上創建一個。
所以我去了server.js文件,我已經添加了我的靜態文件
app.use(express.static(path.join(__dirname,'dist/MDProject')));
app.use('/',(req,res)=>{
res.sendFile(path.join(__dirname,'dist/MDProject/index.html'))
});
點擊這里閱讀我的server.js文件
這樣,當我重定向到本地主機時,它就打開了,但是在server.js端,我沒有任何響應。
誰能幫我解決這個問題
我認為您的代碼中有2處錯誤。
首先,更新您的代碼
從:-
app.use('/',(req,res)=>{
res.sendFile(path.join(__dirname,'dist/MDProject/index.html'))
});
到:- 此代碼需要在您的app.listen(app.get('port'))
代碼之前添加
app.get('*', function(req, res) {
res.sendfile('./public/MDProject/index.html');
// load the single view file (angular will handle the page changes on the front-end)
});
其次,從您服務您的文件public
文件夾(在你的情況,我認為它是dist
文件夾)。 因此,像這樣更新您的代碼。
app.use(express.static(__dirname + '/dist/MDProject'));
在我的server.js文件中刪除此行之后
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
然后在創建端口之前粘貼此行。
app.use(express.static(path.join(__dirname,'dist/MDProject')));
app.use('/',(req,res)=>{
res.sendFile(path.join(__dirname,'dist/MDProject/index.html'))
});
現在工作正常。
用它去索引頁。
app.get('*', (req, res) => {
res.sendfile('./public/MDProject/index.html');
})
這樣,您必須在節點中將文件夾公開
app.use('/', express.static('/dist/MDProject'))
然后在您的應用程序dist文件夾中-> index.html
<base href="http://192.168.1.1:3000/admin-panel/">
這是我的工作代碼。 讓我知道您是否還有任何問題。 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.