[英]Bundle Angular Universal app with custom nodejs/express server
I just want to know the correct way to serve an angular universal app with an existing nodejs/express server that talks to a Mongodb database and serves data with an "/api/*" route for example. 我只想知道使用现有的nodejs / express服务器与Mongodb数据库对话并使用“ / api / *”路由提供数据的角度通用应用程序的正确方法。
So is it to have the universal app have its own node/express server as explained here https://angular.io/guide/universal and just merge that with the existing node/express server with route configuration as done here Serve angular universal app with NodeJS/ExpressJS back-end and allow both the custom node/express server and the universal apps express server have different ports (because i assume that is how they will be able to co-exist in production) 因此,它是有普遍的应用程序有它自己的节点/ Express服务器按如下说明https://angular.io/guide/universal ,只是合并与现有节点/表达与路由配置服务器为这里所做即成角通用的应用程序与NodeJS / ExpressJS后端配合使用,并允许自定义节点/表达服务器和通用应用程序表达服务器具有不同的端口(因为我认为这是它们将能够在生产环境中共存的方式)
Or do we use the custom node/express server as the server for the universal app with proper configuration. 还是我们使用自定义节点/表达服务器作为具有适当配置的通用应用程序服务器。
To serve angular app with node server first you need to build the app just run the below command 要将角度应用程序与节点服务器一起提供,您需要构建该应用程序,只需运行以下命令
ng build
And then add two lines of code in your node server file as 然后在您的节点服务器文件中添加两行代码,如下所示:
app.use(express.static(__dirname + '/dist'))
app.use(function(req, res) {
res.sendFile(__dirname + '/dist/index.html')
})
And after that you can run node server and the default entry index html file will open when you hit the host. 然后,您可以运行节点服务器,并且在您击中主机时将打开默认的条目索引html文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.