如何用express替换angular-cli服务器? [英] How to replace angular-cli server with express?
问题描述
我想用 express 替换 angular-cli 中的内置服务器,以便我可以编辑 express javascript 以添加我需要在服务器上执行的小部件.怎么做?
I would like to replace the built in server in angular-cli with express, so that I can edit the express javascript to add widgets I need executing on the server. How can that be done?
推荐答案
我在angular-cli生成的src文件夹下添加了一个app.js文件,写了一个简单的server.当你运行 ng build 时,你添加到 src 和 public 目录中的文件将被编译并添加到 dist 目录中.所以你应该相对于 dist 中的 index.html 文件渲染/发送:
I added an app.js file to the src folder generated by the angular-cli, and wrote a simple server. When you run ng build, the files you add to the src and public directories will be complied and added to the dist directory. So you should render/send relative to the index.html file in dist:
app.get('/*', function (req, res) {
res.sendFile(path.join(__dirname,'index.html'))
});
从终端运行 express 服务器:$ node dist/app.js
To run the express server from the terminal: $ node dist/app.js
当我尝试嵌套 app.js 文件时遇到一些编译器错误:src/server/app.js,但所有其他服务器文件在嵌套时都可以正常工作,例如模型和路由.
I get some compiler errors when I try to nest the app.js file: src/server/app.js, but all the other server files work fine when nested, such as models and routes.
这篇关于如何用express替换angular-cli服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!