node.js - 如何在生产环境中使用nodejs api 运行 Angular 6 应用程序?

标签 node.js angular angular6 node-request

嗨,我是 Angular6 的新手

在开发模式下,我在操作中使用了 Angular 4200 端口和不同端口(8080)的 Node 。 但现在我想将我的应用程序转移到生产模式。 为此,我使用 ng build 命令构建了我的 Angular 项目。在我得到一个 dist 文件夹后,在根文件夹上创建了一个 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 端我没有得到任何响应。

每当我尝试登录时,都会收到此错误。 enter image description here

谁能帮我解决这个问题

最佳答案

我认为您的代码中有两处错误。

首先,更新您的代码

来自:-

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'));

关于node.js - 如何在生产环境中使用nodejs api 运行 Angular 6 应用程序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53099973/

相关文章:

javascript - 拥有公共(public) API,但只允许访问从我的网站发送的请求

node.js - 我正在创建一个静态 Node.js 服务器

node.js - typescript 和 Node https.get()

javascript - Angular 2中路线更改时的结束间隔

angular - 拦截输入属性更改的命名约定

javascript - 2 个数组,将一个数组的 id 添加到另一个数组,其中它们具有相同的值

css - 使 mat-card-image 中的所有图像大小相同但正确缩放

Angular 2+ i18n 方言/口音

javascript - Mat-select 不显示所选值

angular - 如何将数据从 Angular Material 对话框传递到父组件?