angular-cli 带有一个 express 项目

angular-cli with an express project

我有一个使用 Express、MongoDB 和 REST API 的项目。现在我想使用 angular-cli 添加到项目 Angular。我有一些关于我应该怎么做的问题。

这是我当前的项目结构:

1) 我应该在哪个文件夹中使用 ng new?

2) 现在我使用 nodemon 在 http://localhost:3000/ but Angular starts at http://localhost:4200 启动服务器。如何使 Angular 与 express 在同一台服务器上运行并像以前一样使用 nodemon 启动服务器?

3) expressnode_modules和angularnode_modules做的时候如何区分npm 安装?

4) 从 angular 页面访问 api 需要执行哪些步骤?

  1. 你可以把它放在任何地方(不完全相同的快递文件夹)
  2. 假设我们在 http://localhost:3000/api and we want all calls to http://localhost:4200/api 上有一个服务器 运行 去那个服务器。

我们在我们项目的 package.json 旁边创建一个名为 proxy.conf.json 的文件,内容为

{
  "/api": {
    "target": "http://localhost:3000",
    "secure": false
  }
}

您可以在 https://github.com/angular/angular-cli/blob/master/docs/documentation/stories/proxy.md

中阅读更多内容
  1. 只需使用 2 个不同的文件夹

  2. 代理后你可以像代理的URL路径一样调用它

在所有这一切之后,你只需要记住 angula 是一个你可以构建的软糖,它将呈现一个 html 和 js 你可以托管这个 html,你的 express 服务器中的 js