如何用Express取代angular-cli服务器? [英] How to replace angular-cli server with express?

查看:60
本文介绍了如何用Express取代angular-cli服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用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?

推荐答案

我将app.js文件添加到由angular-cli生成的src文件夹中,并编写了一个简单的服务器.当您运行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'))
});

要从终端运行快递服务器:$ 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆