Set default host and port for ng serve in config file

后端 未结 12 2156
庸人自扰
庸人自扰 2020-12-07 07:53

I want to know if i can set a host and a port in a config file so I don\'t have to type

ng serve --host foo.bar --port 80

instead of just

相关标签:
12条回答
  • 2020-12-07 08:24

    Another option is to run ng serve command with the --port option e.g

    ng serve --port 5050 (i.e for port 5050)

    Alternatively, the command: ng serve --port 0, will auto assign an available port for use.

    0 讨论(0)
  • 2020-12-07 08:31

    This changed in the latest Angular CLI.

    The file name changed to angular.json, and the structure also changed.

    This is what you should do:

    "projects": {
        "project-name": {
            ...
            "architect": {
                "serve": {
                    "options": {
                      "host": "foo.bar",
                      "port": 80
                    }
                }
            }
            ...
        }
    }
    
    0 讨论(0)
  • 2020-12-07 08:32

    here is what i put into package.json (running angular 6):

    {
      "name": "local-weather-app",
      "version": "1.0.0",
      "scripts": {
        "ng": "ng",
        "start": "ng serve --port 5000",
        "build": "ng build",
        "test": "ng test",
        "lint": "ng lint",
        "e2e": "ng e2e"
      },
    

    Then a plain npm start will pull in the contents of start. Could also add other options to contents

    0 讨论(0)
  • 2020-12-07 08:33

    As of right now that feature is not supported, however if this is something that bothers you an alternative would be in your package.json...

    "scripts": {
      "start": "ng serve --host foo.bar --port 80"
    }
    

    This way you can simply run npm start

    Another option if you want to do this across multiple projects is to create an alias, which you can potentially name ngserve which will execute your above command.

    0 讨论(0)
  • 2020-12-07 08:36

    If you want to specifically have your local ip address open when running ng serve you can do the following:

    npm install internal-ip-cli --save
    ng serve --open --host $(./node_modules/.bin/internal-ip --ipv4)
    
    0 讨论(0)
  • 2020-12-07 08:40

    We have two ways to change default port number in Angular.

    First way is by CLI command:

    ng serve --port 2400 --open

    Second way is by configuration at the location:

    ProjectName\node_modules\@angular-devkit\build-angular\src\dev-server\schema.json.

    Make changes in schema.json file.

    {
     "title": "Dev Server Target",
      "description": "Dev Server target options for Build Facade.",
      "type": "object",
      "properties": {
        "browserTarget": {
          "type": "string",
          "description": "Target to serve."
        },
        "port": {
          "type": "number",
          "description": "Port to listen on.",
          "default": 2400
        },
    
    0 讨论(0)
提交回复
热议问题