Establecer el host y el puerto predeterminados para el servicio ng en el archivo de configuración


Quiero saber si puedo establecer un host y un puerto en un archivo de configuración para no tener que escribir

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

En lugar de solo

ng serve
Author: Marek Grác, 2016-06-11

8 answers

A partir de al menos Angular CLI 1.0 ahora puede establecer estos directamente en angular.json (o angular-cli.json) debajo del elemento defaults:

{
  "defaults": {
    "serve": {
      "port": 4444,
      "host": "10.1.2.3"
    }
  }
}
 144
Author: Toby J,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-08-14 13:59:47

A partir de ahora esa característica no está soportada, sin embargo, si esto es algo que le molesta, una alternativa estaría en su paquete.json...

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

De esta manera, simplemente puede ejecutar npm start

Otra opción si desea hacer esto en varios proyectos es crear un alias, que potencialmente puede nombrar ngserve que ejecutará el comando anterior.

 48
Author: Brocco,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-06-13 13:08:55

Puede configurar el puerto HTTP predeterminado y el utilizado por el servidor LiveReload con dos opciones de línea de comandos:

ng serve --host 0.0.0.0 --port 4201 --live-reload-port 49153

Https://github.com/angular/angular-cli

 25
Author: Shreekant N,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-03 07:24:32

Otra opción es ejecutar el comando ng serve con la opción --port, por ejemplo

ng serve --port 5050 (es decir, para el puerto 5050)

Alternativamente, el comando: ng serve --port 0, asignará automáticamente un puerto disponible para su uso.

 10
Author: Mwizak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-10-03 10:39:04

Esto cambió en la última CLI de Angular.

El nombre del archivo cambió a angular.json, y la estructura también cambió.

Esto es lo que debes hacer:

"projects": {
    "project-name": {
        ...
        "architect": {
            "serve": {
                "options": {
                  "host": "foo.bar",
                  "port": 80
                }
            }
        }
        ...
    }
}
 8
Author: arsanyf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-04 13:46:20

Puedes guardarlos en un archivo, pero tienes que ponerlo en .ember-cli (al menos por el momento); ver https://github.com/angular/angular-cli/issues/1156#issuecomment-227412924

{
"port": 4201,
"liveReload": true,
"host": "dev.domain.org",
"live-reload-port": 49153
}

Editar: ahora puede configurarlos en angular-cli.json a partir de commit https://github.com/angular/angular-cli/commit/da255b0808dcbe2f9da62086baec98dacc4b7ec9 , que está en la compilación 1.0.0-beta.30

 6
Author: Dan Mitchell,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-02-08 15:24:23

Esto es lo que pongo en el paquete.json (ejecución 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"
  },

Entonces un inicio npm plano extraerá el contenido de inicio. También podría añadir otras opciones a contents

 0
Author: John Dugger,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-26 12:24:34

Introduzca aquí la descripción de la imagen

Solo tienes que hacer una cosa. Escriba esto en el Símbolo del sistema: ng serve port puerto 4021 [o cualquier otro puerto que desee asignar, por ejemplo: 5050, 5051, etc]. No es necesario hacer cambios en los archivos.

 0
Author: Shahbaz Ali Khan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-10-04 00:00:44