简体   繁体   English

由于路径相对性问题,无法从快递服务器提供angular 2 index.html文件

[英]Cannot serve angular 2 index.html file from express server due to path relativity issue

图。1

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.

相关问题 Angular 404(未找到)- 无法从 Angular 应用程序中的“index.html”页面加载 Javascript 文件 - Angular 404 (Not Found)- Cannot Load Javascript file from 'index.html' page in Angular Application 仅从index.html请求的快速文件服务 - Express file serving only requested from index.html 使用 angular 中的 index.html 加强扫描问题 - Fortify scan issue with index.html in angular 将JSON从索引操作提供到公用文件夹中的index.html - Serve JSON from index action to index.html in public folder build/index.html 是否可以用作没有服务器的独立网页? - Does build/index.html serve as a standalone webpage without server? 尝试访问index.html时Express Server返回“无法获取” - Express Server returns Cannot Get when trying to access index.html Express 正在提供 index.html 而不是我的 Webpack 包。 我的配置文件有问题吗? - Express is serving up index.html instead of my Webpack bundle. Is the issue with my config file? MeanJS-将Angular集成到index.html文件中 - MeanJS - integrate Angular in index.html file webpack + express + angular 4错误“错误:ENOENT:没有这样的文件或目录,统计信息'C:\\ public \\ index.html' - webpack + express + angular 4 error "Error: ENOENT: no such file or directory, stat 'C:\public\index.html' Express.js 服务生成的 index.html 问题 - Express.js serving generated index.html issue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM