node.js npm start 如何在端口 8000 上运行服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24750253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 17:38:57  来源:igfitidea点击:

How npm start runs a server on port 8000

node.jsangularjs

提问by user3724677

I recently used a angular-seed folder from github for angular application development. In some previous angularjs tutorial there was a script folder and a server.js file in the angular-seed folder which had all the configuration for running the node server. So how does npm now just start running a node server and where is all the configuration of that node server?

我最近使用来自 github 的 angular-seed 文件夹进行角度应用程序开发。在之前的一些 angularjs 教程中,angular-seed 文件夹中有一个脚本文件夹和一个 server.js 文件,其中包含运行节点服务器的所有配置。那么 npm 现在如何开始运行一个节点服务器以及该节点服务器的所有配置在哪里?

回答by Mritunjay

If you will look at package.jsonfile.

如果你会看package.json文件。

you will see something like this

你会看到这样的东西

 "start": "http-server -a localhost -p 8000"

This tells start a http-serverat address of localhoston port 8000

这告诉 start a http-serverat address of localhoston port8000

http-serveris a node-module.

http-server是一个节点模块。

Update:-Including comment by @Usman, ideally it should be present in your package.jsonbut if it's not present you can include it in scriptssection.

更新:-包括@Usman 的评论,理想情况下它应该存在于您的中,package.json但如果不存在,您可以将其包含在scripts部分中。

回答by YeeHaw1234

We have a react application and our development machines are both mac and pc. The start command doesn't work for PC so here is how we got around it:

我们有一个 react 应用程序,我们的开发机器是 mac 和 pc。start 命令对 PC 不起作用,所以我们是这样解决的:

"start": "PORT=3001 react-scripts start",
"start-pc": "set PORT=3001&& react-scripts start",

On my mac:

在我的 Mac 上:

npm start

On my pc:

在我的电脑上:

 npm run start-pc

回答by Kotireddy

To change the port

更改端口

npm start --port 8000

回答by Skylin R

You can change the port in the console by running the following on Windows:

您可以通过在Windows上运行以下命令来更改控制台中的端口:

SET PORT=8000

For Mac, Linuxor Windows WSLuse the following:

对于MacLinuxWindows WSL,请使用以下内容:

export PORT=8000

The exportsets the environment variable for the current shell and all child processes like npmthat might use it.

出口将针对当前的外壳和所有子进程,如环境变量NPM可能会使用它。

If you want the environment variable to be set just for the npmprocess, precede the command with the environment variable like this (on Macand Linuxand Windows WSL):

如果您希望仅为npm进程设置环境变量,请在命令前添加这样的环境变量(在MacLinux以及Windows WSL 上):

PORT=8000 npm run start

回答by theocikos

To start the port correctly in your desired port use:

要在所需的端口中正确启动端口,请使用:

npm start -- --port 8000

npm start -- --port 8000