配置角度cli构建监视模式和nodemon监视的最佳方法是什么?

时间:2017-05-25 06:20:26

标签: angular express mean-stack angular-cli nodemon

您好我正在开发一个使用角度4的平均应用程序。我使用了角度cli配置。我想使用监视模式自动构建代码,并且还希望在任何文件更改时重新启动节点服务器。

我使用了以下命令但没有工作

with open(filesource, 'w+') as target:
    print "Provide 3 lines for the file"

    line1 = "aaaaaaa"
    line2 = "bbbbbbb"
    line3 = "ccccccc"

    target.write (("%s\n%s\n%s\n") % (line1, line2, line3))
    target.seek(0)
    print target.read()

我已将配置保存在目录script: { "server": "ng build -w && nodemon --watch src/**/*.ts --exec ts-node ./bin/www" } 文件中,该文件从其他位置导入bin/www

上述命令的问题是,ng cli正在构建启用了watch的代码,但nodemon没有启动。

所以我尝试了以下内容。但它的建筑只有一次作为手表没有启用cli。

server.ts
  

nodemon watch在任何一种情况下都不起作用。

2 个答案:

答案 0 :(得分:5)

根据上面的评论,这就是我使两台服务器工作并响应任何变化的方式。

  • 使用angular-cli工具,前端服务器
  • 创建Angular 2应用
  • 在此应用程序的根目录中创建server.js文件,后端服务器,Express应用程序示例为here
  • 安装软件包 npm-run-all 以同时启动2台服务器
  • 使用以下内容在应用程序的根目录中创建文件server.conf.json
{
  "/api": {
    "target": "http://localhost:3000",
    "secure": false
  }
}
  • 修改package.json以使用此部分
"scripts": {
  "client": "ng serve --proxy-config server.conf.json",
  "server": "nodemon server.js",
  "start": "npm-run-all -p client server"
}
  • run" npm start"

现在 ng服务正在观看Angular和 nodemon 中的任何更改 - 在Node中。

localhost:4200                  // ng serve => angular
localhost:4200/page             // ng serve => angular
localhost:4200/another/page     // ng serve => angular
localhost:4200/api              // ng serve => node
localhost:4200/api/users        // ng serve => node
localhost:3000/api              // node
localhost:3000/api/users        // node

答案 1 :(得分:0)

<强>的package.json

"scripts": {
    "start": "concurrently --kill-others \"npm run server:run\" \"npm run server\" \"ng serve --aot --progress=false --proxy-config proxy.conf.json\"",
    "lint:client": "ng lint",
    "lint:server": "tslint './server/**/*.ts' -c ./server/tslint.json --fix",
    "test:client": "ng test --watch false",
    "e2e:client": "ng e2e",
    "build": "ng build --prod --sm=false --aot --output-path=dist && npm run server:build",
    "server:run": "nodemon",
    "server": "node server.js",
    "server:build": "tsc -p ./server",
    "postinstall": "npm run build",
    "build:prod":"ng build --prod --sm=false --aot --output-path=dist"
  }

<强> proxy.conf.json

 {
      "/api/*": { // need to change api with your specific name appropriate to your http 
        "target": "http://localhost:3000",
        "secure": false
      }
    }

然后 npm start npm run start