从 Hapi js 启动 angular 4
Launch angular 4 from Hapi js
我的后端使用 hapi js,前端使用 angular 4. 我有 2 个服务器来启动我的前端和后端。
我想从 hapi js 启动我的 angular 应用程序,但我不知道该怎么做。我找到了 https://github.com/guillaume-chs/angular4-hapijs,但它不起作用。你有更多的例子吗?
谢谢
我猜 hapi.js 是父项目而 angular 4 只是 "view",所以你需要做的是 GET /
服务静态index.html
文件在 angular 构建后位于 dist
文件夹下。
编辑:在 node.js 应用中 angular 作为视图项目我使用这个 res.sendFile(${frontEndDist}/index.html)
ngOnInit()
也许不需要 if
语句
this.http.get(this.portURL)
.toPromise()
.then((res: any) => {
const json = res.json();
this.port = json.port;
console.log(`port ${this.port}`);
this.url = location.origin.replace(/^http/, 'ws');
if (location.origin.includes('localhost')) {
this.url = this.url.replace('4200', '5000');
} else {
this.url = this.url.replace('4200', this.port.toString());
}
this.initWebSocketConnection();
console.log(`url ${this.url}`);
})
.catch((reason) => {
console.log(reason);
});
在 node.js 应用中
const server = express()
.use(express.static(frontEndDist))
.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
})
.get('/port', (req, res) => {
res.json({port: port})
})
.get('*', (req, res) => {
res.sendFile(`${frontEndDist}/index.html`);
})
.listen(port, () => {
console.log(`Listening on ${port}`)
});
我想您可以轻松地将其转换为 hapi.js 语法 - 与 reply.file(//path to index.html)
相关的内容
我的后端使用 hapi js,前端使用 angular 4. 我有 2 个服务器来启动我的前端和后端。
我想从 hapi js 启动我的 angular 应用程序,但我不知道该怎么做。我找到了 https://github.com/guillaume-chs/angular4-hapijs,但它不起作用。你有更多的例子吗?
谢谢
我猜 hapi.js 是父项目而 angular 4 只是 "view",所以你需要做的是 GET /
服务静态index.html
文件在 angular 构建后位于 dist
文件夹下。
编辑:在 node.js 应用中 angular 作为视图项目我使用这个 res.sendFile(${frontEndDist}/index.html)
ngOnInit()
也许不需要 if
语句
this.http.get(this.portURL)
.toPromise()
.then((res: any) => {
const json = res.json();
this.port = json.port;
console.log(`port ${this.port}`);
this.url = location.origin.replace(/^http/, 'ws');
if (location.origin.includes('localhost')) {
this.url = this.url.replace('4200', '5000');
} else {
this.url = this.url.replace('4200', this.port.toString());
}
this.initWebSocketConnection();
console.log(`url ${this.url}`);
})
.catch((reason) => {
console.log(reason);
});
在 node.js 应用中
const server = express()
.use(express.static(frontEndDist))
.use((req, res, next) => {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
})
.get('/port', (req, res) => {
res.json({port: port})
})
.get('*', (req, res) => {
res.sendFile(`${frontEndDist}/index.html`);
})
.listen(port, () => {
console.log(`Listening on ${port}`)
});
我想您可以轻松地将其转换为 hapi.js 语法 - 与 reply.file(//path to index.html)