【发布时间】:2018-01-30 01:36:51
【问题描述】:
我是 NodeJS 和 Express 的新手,但我想要一个简单的“/”路由到 Angular 的默认 index.html (client/src/index.html),其中包含 app-root 标记。
“/”路由成功地为 index.html 文件提供服务,但它没有将“app-root”标签扩展/加载到组件的 html 中,所以我只是得到空白页。
我不确定为什么从 Express 路由时它无法解析 app-root 标记。只有当我通过 'ng serve' 运行 Angular 时,index.html 才会成功加载 app-root 标记的内容。
我的结构如下:
/client
/e2e
/node_modules
/app
app.component.css/html/ts
app.module.ts
/src
index.html
main.ts
package.json
server.js
server.js
var express = require('express');
var path = require('path');
var port = 80;
var app = express();
// set static folder
app.use(express.static(path.join(__dirname, '/client')));
app.get('/', function(req, res, next){
res.sendFile(__dirname + '/client/src/index.html');
});
app.listen(port, function(){
console.log("Server started on port " + port);
});
【问题讨论】:
-
您需要设置类似 on get app/root return that directory back to the request
-
你的 index.html 文件是什么样的?
-
抱歉,你能告诉我那会是什么样子吗?
-
Angular index.html是默认的html模板,body中只有一个app-root标签,组件的html是创建模板时的默认模板