2017-04-23 144 views
1

我试图在同一台服务器上使用angular2应用程序托管我的快递服务。 阅读相关变化后,我发现变化后的​​解决方案都解决本地服务器http://localhost:3000上,但我托管在Heroku上的应用程序时,我尝试访问我的快递路线与/ API启动/ ... ...所有运作良好。但无法访问它开始在页面上显示Not Found消息的angular2 UI页面。如何使angular2路由与express路由器一起工作?

app.all('*', (req, res) => { 
    console.log(`[TRACE] Server 404 request: ${req.originalUrl}`); 
    res.sendFile(path.join(__dirname, 'dist/index.html')); 
}); 

app.use(function(req, res, next) { 
    res.sendFile(path.join(__dirname, 'dist/index.html')); 
}); 

app.get('/*', function (req, res) { 
    res.sendFile(path.join(__dirname, 'dist/index.html')); 
}); 

app.get(/^\/([^a]|a[^p]|ap[^i]|api[^/]).*$/,(req,res) => { 
    res.sendFile(path.join(__dirname, 'dist/index.html')); 
}); 
+0

什么是“我想访问我的快递路线与/ API开始部署/ ...一切运作良好,但无法访问angular2 UI它开始在页面上显示Not Found消息。“意思?什么不起作用? – echonax

+0

它没有显示我的angular2应用程序登录页面。 –

+0

这几乎是不可能猜测错误是基于你给出的信息。请分享你的文件夹结构,你的路由细节,也许你的Heroku的网址,如果它不是私人 – echonax

回答

1

当我部署我的应用程序的托管服务器,我发现dist目录中没有得到部署在服务器包含Angular应用程序构建,获取cr跑完ng build - 产品

首先,我手动将其上传到托管服务器,我的应用程序开始正常工作。然后在我看来,每次都是手动任务,所以我找到了解决这个问题的两个解决方案。

  1. 首先,从.gitignore文件中删除/ dist项,这意味着当我将应用程序推送到任何托管服务器时,它不会忽略dist目录。
  2. 其次,我从这个链接https://docs.npmjs.com/misc/scripts阅读的package.json文件,了解有关安装前和安装后脚本,并更新了我的package.json文件有以下脚本。

    "scripts": { 
        "ng": "ng", 
        "start": "node index.js", 
        "build": "ng build", 
        "test": "ng test", 
        "lint": "ng lint", 
        "e2e": "ng e2e", 
        "postinstall": "ng build --prod" 
    }, 
    

阅读这篇文章在Heroku https://paucls.wordpress.com/2016/11/25/deploy-angular-2-cli-app-to-heroku/