[英]Cannot serve angular 2 index.html file from express server due to path relativity issue
This is my app folder structure with the angular-client folder 这是我的应用程序文件夹结构,带有angular-client文件夹
The below picture shows the issue where when I server the index file of angular app from express server , all the gets are relative to the express server app. 下图显示了当我从快递服务器上服务角度应用程序的索引文件时,所有获取都与快递服务器应用程序有关的问题。
This is index.js server code 这是index.js服务器代码
var express = require('express');
var app = express();
var path = require('path');
app.listen(3000,function(){
console.log('Server started on port 3000');
});
app.get('/',function(req,res){
res.sendFile(path.join(__dirname,'angular-client')+'/src/index.html');
});
Could anyone find a solution ? 有人可以找到解决方案吗?
The route in your image only handles GET /
(and serves index.html
as the response). 图片中的路由仅处理
GET /
(并将index.html
作为响应)。
First, you need a route that catches GET /styles.css
, for example. 首先,您需要一个捕获
GET /styles.css
的路由。 Then you need to have it serve the styles.css
as the response. 然后,您需要让它充当
styles.css
作为响应。
express.static does both of these things for you (see the docs in the link). express.static为您完成了这两项操作(请参阅链接中的文档)。
Assuming you run node server.js
from the TODOMEANAPP
directory (as opposed to, say, node TODOMEANAPP/server.js
), the following code should work: 假设您从
TODOMEANAPP
目录(而不是node TODOMEANAPP/server.js
)运行node server.js
,则以下代码应该工作:
app.use(express.static('angular-client'));
However, you'll have to change href="styles.css"
to href="src/styles.css"
and src="systemjs.config.js"
to src="src/systemjs.config.js"
. 但是,您必须将
href="styles.css"
更改为href="src/styles.css"
,将src="systemjs.config.js"
更改为src="src/systemjs.config.js"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.