[英]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.