简体   繁体   English

为什么我从运行指向Angular应用程序的/ dist文件夹的节点服务器时得到空白页?

[英]Why I'm getting a blank page from running a node server that point to the /dist folder of an Angular app?

My simple node server is: 我的简单节点服务器是:

const express = require('express');
const app = express();

app.get('*', (req, res) => {
  res.sendFile(__dirname + '/dist/page/index.html');
})

app.listen(3335, () => { console.log('Server is listening on 3335'); });

But I'm getting the index file that seems not running the main.js The Angular app, at the moment it's literally a page/component that is app.component.ts, so there is not any routing. 但是我得到的索引文件似乎没有运行main.js Angular应用程序,目前它实际上是一个页面/组件,即app.component.ts,因此没有任何路由。

在此处输入图片说明

Use express.static() for serving static files. 使用express.static()提供静态文件。

It is because you have set a response of 'index.html' file for each and every request the server would receive. 这是因为您为服务器将收到的每个请求都设置了“ index.html”文件的响应。 The first response would be good that's the index.html page only as expected. 第一个响应会很好,那就是只有预期的那样才是index.html页面。 But, the index.html page must be having some script and css tags to fetch your Angular Javascript code which I assume would be on the same node server. 但是,index.html页面必须具有一些脚本css标记才能获取您的Angular Javascript代码,我认为它们将在同一节点服务器上。 So when the browser would encounter a line like: 因此,当浏览器遇到如下一行时:

<script src="/angularApp.js"></script>

..in your index.html file while parsing it, it would make another request to the node server for http://localhost:<port>/angularApp.js but would get the index.html file as the response as that is what you have set. ..在解析它的index.html文件中时,它将向节点服务器发出另一个对http://localhost:<port>/angularApp.js但会得到index.html文件作为响应,因为你已经设定了。

Do it like this to serve static files like .html, .css, .js or what have you: 这样做是为了提供静态文件,例如.html,.css,.js或您拥有的文件:

app.use(express.static(path.join(__dirname, '/dist')));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Gulp将Node应用程序的dist文件夹部署到Heroku - Gulp deploy dist folder of Node app to Heroku 我正在尝试将pdf文件从节点服务器下载到React Client,但是当我打开它时,它显示为空白 - I'm trying to download a pdf file from a node server to a react client but when I open it, it shows blank 构建单spa根节点应用程序时未创建Dist文件夹 - Dist folder is not getting created while building a single-spa root node app 使用Node Js服务器运行Angular2应用程序 - Running Angular2 app with Node Js server 为什么我无法从运行在端口 3000 上的 React 应用程序访问运行在端口 3001 上的节点服务器? - Why am I unable to access my node server running on port 3001 from my React app running on port 3000? 如何在 Kubernetes 中部署 Node JS 应用程序以及用于生产的 dist 文件夹? - How can I deploy Node JS app along with dist folder for production in Kubernetes? 如何确定为什么我的node.js服务器上出现503错误? - How do I determine why I'm getting 503 errors on my node.js server? 使用dist文件夹时,Angular 5无法从NodeJs,Mongodb获取数据 - Angular 5 not getting the data from NodeJs, Mongodb when dist folder has been used 为什么我收到错误 [ERR_MODULE_NOT_FOUND]:运行服务器时找不到模块 - Why I'm getting Error [ERR_MODULE_NOT_FOUND]: Cannot find module when running the server 从node.js服务器运行angular - Running angular from node.js server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM