简体   繁体   English

在单个 Azure Web 应用程序中具有 Angular 前端的 Node/Express 后端

[英]Node/Express Backend with Angular Front End in a Single Azure Web App

I have a Node/Express setup in dev that outputs data as JSON for consumption.我在 dev 中有一个 Node/Express 设置,它将数据输出为 JSON 以供使用。 In dev that endpoint is localhost:3000/data在开发中,端点是 localhost:3000/data

I also have an Angular 8 app in the same node directory for the front end.我在前端的同一节点目录中还有一个 Angular 8 应用程序。 In dev, I launch two separate Node command prompts... one to run node/express at port 3000 and another to run angular at port 4200.在开发中,我启动了两个单独的 Node 命令提示符……一个在端口 3000 运行 node/express,另一个在端口 4200 运行 angular。

The goal is to have the Angular app output the data the Node/Express backend is providing... in an Azure Web App.目标是让 Angular 应用程序输出 Node/Express 后端提供的数据......在 Azure Web 应用程序中。

I have read articles that state how to deploy Angular to a WebApp, but can I have both the Node/Express backend serving data and the Angular app running in a single Azure Web App... or do I need to create two separate apps and use a CORS listing for the frontend to speak to the backend Web App?我已经阅读了说明如何将 Angular 部署到 WebApp 的文章,但是我是否可以在单个 Azure Web 应用程序中同时运行 Node/Express 后端服务数据和 Angular 应用程序...或者我是否需要创建两个单独的应用程序和使用前端的 CORS 列表与后端 Web 应用程序通话?

I am guessing that I will need to use Express to launch Angular then perform a build instead of Express and Angular running on separate ports?我猜我需要使用 Express 来启动 Angular,然后执行构建而不是 Express 和 Angular 在单独的端口上运行?

You can use Angular and Express on the same directory, and a middleware on express, so when you do request, the middleware identify if it's a "/data" URL or a simple URL and send the index file that angular generate on dist folder.您可以在同一目录中使用 Angular 和 Express,并在 express 上使用中间件,因此当您发出请求时,中间件会识别它是“/data” URL 还是简单的 URL,并将 angular 生成的索引文件发送到 dist 文件夹中。

  1. Move app.js to your Angular app folder将 app.js 移动到你的 Angular 应用文件夹
  2. Insert this code;插入此代码;
     app.use(express.static(path.join(__dirname, '/dist/<your Angular App Name>')));

     app.use('/data', <your route file>);
     app.use(function(req, res) {
     res.sendFile(path.join(__dirname, '/dist/<your Angular App Name>', 'index.html'));

With this, you Angular and Express runs on localhost:3000.有了这个,你的 Angular 和 Express 在 localhost:3000 上运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Azure web app multi container (MEAN app),从前端容器连接到节点后端容器的URL是什么? - Azure web app multi container (MEAN app), what is the URL to connect to node backend container from front end container? 博客 web 中的身份验证问题,后端使用节点、快递和 mongoose 和前端使用 ejs - authentication problem in blogging web, backend with node, express and mongoose and front end with ejs 将数据从 Vue 前端传递到 Node/Express 后端 - Pass data from Vue front end to Node/Express backend 为 node js 后端 API 创建 Express 前端 - Creating Express front end for node js backend API 我的应用程序的基本身份验证-Node JS和Mongo作为后端,而Angular作为前端 - Basic authentication for my app - Node JS & Mongo as backend and Angular as front end 我的 React 前端无法调用我的 node-express 后端服务器,全栈应用程序部署在 heroku - My React front-end is unable to call my node-express backend server, the fullstack app is deployed in heroku Express,Node,Angular将音频文件发送到前端 - Express, Node, Angular sending audio file to front end 连接 react 前端和 express 后端 - Connecting react front end and express backend PUG NodeJS Express 前端/后端无连接 - PUG NodeJS Express Front End / Backend no connection 如何将数据从反应前端传递到节点快递后端? - How do I pass data from react front end to node express backend?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM