简体   繁体   English

直接在 Angular ng serve 中运行后端

[英]Run backend directly in Angular ng serve

I'm currently developing using ng serve , with proxy configuration, while the proxy points to another nodejs instance on the same machine.我目前正在使用ng serve开发,并带有代理配置,而代理指向同一台机器上的另一个 nodejs 实例。

This backend is a simple express server, like this (simplified):这个后端是一个简单的快速服务器,像这样(简化):

var express = require('express');
var app = express();
var customers = require('./customers.controller.js');
app.get('/api/customers', customers.getAll)
var server = app.listen(8081)

The frontend ( ng serve ) runs on port 4200 and proxies /api to http://localhost:8081/api前端 ( ng serve ) 在端口 4200 上运行,并将 /api 代理到http://localhost:8081/api

As far as I can see, this is the recommended setup.据我所知,这是推荐的设置。

However, I would prefer to have the backend running directly inside of ng serve instance instead of the proxy.但是,我更愿意让后端直接在ng serve实例中运行,而不是在代理中运行。 And if possible, even take advantage of the automatic reload feature of ng so that I don't have to restart the server if I change something on the backend code.如果可能的话,甚至可以利用 ng 的自动重新加载功能,这样如果我对后端代码进行了更改,我就不必重新启动服务器。

As both are nodejs and ng seems to be configurable, I think this is possible, but I can't find a starting point for defining my own routes由于两者都是 nodejs 并且ng似乎是可配置的,我认为这是可能的,但我找不到定义我自己的路由的起点

Its possible to do this you just need to put your angular into the backend by utilize the nodejs routing可以做到这一点,您只需要利用 nodejs 路由将角度放入后端

basicly angular is a "static file" and the entry point is coming from the index.html基本上 angular 是一个“静态文件”,入口点来自 index.html

    // Redirect all the other resquests
    this.app.get('*', (req, res) => {
        res.sendFile(path.resolve('dist/index.html'));
    });

but remember you need to handle the routing for image, js, css and others also.但请记住,您还需要处理图像、js、css 等的路由。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM