简体   繁体   English

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

[英]How npm start runs a server on port 8000

I recently used a angular-seed folder from github for angular application development.我最近使用 github 的 angular-seed 文件夹进行 angular 应用程序开发。 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.在之前的一些 angularjs 教程中,angular-seed 文件夹中有一个脚本文件夹和一个 server.js 文件,其中包含运行节点服务器的所有配置。 So how does npm now just start running a node server and where is all the configuration of that node server?那么 npm 现在如何开始运行节点服务器,该节点服务器的所有配置在哪里?

If you will look at package.json file.如果您查看package.json文件。

you will see something like this你会看到这样的东西

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

This tells start a http-server at address of localhost on port 8000这告诉在端口8000上的localhost地址处启动一个http-server

http-server is a node-module. http-server是一个节点模块。

Update:- Including comment by @Usman, ideally it should be present in your package.json but if it's not present you can include it in scripts section.更新:-包括@Usman 的评论,理想情况下它应该存在于您的package.json但如果不存在,您可以将其包含在scripts部分中。

We have a react application and our development machines are both mac and pc.我们有一个 React 应用程序,我们的开发机器是 mac 和 pc。 The start command doesn't work for PC so here is how we got around it: 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

更改端口

npm start --port 8000

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

npm start -- --port 8000

You can change the port in the console by running the following on Windows :您可以通过在Windows上运行以下命令来更改控制台中的端口:

SET PORT=8000

For Mac , Linux or Windows WSL use the following:对于MacLinuxWindows WSL,请使用以下内容:

export PORT=8000

The export sets the environment variable for the current shell and all child processes like npm that might use it.导出为当前 shell 和可能使用它的所有子进程(如npm)设置环境变量。

If you want the environment variable to be set just for the npm process, precede the command with the environment variable like this (on Mac and Linux and Windows WSL ):如果您希望仅为npm进程设置环境变量,请在命令前添加这样的环境变量(在MacLinux以及Windows WSL 上):

PORT=8000 npm run start
npm run dev -- --port 3001

npm run on another port in cmd npm 在 cmd 的另一个端口上运行

"start": "react-scripts start",更改为"start": "set PORT=3001 && react-scripts start",

或者干脆在终端

set port=5500 && npm start

The solution which will work on every machine, let it be MAC, Window, Linux or any other, just specify the port where you want to run.适用于每台机器的解决方案,无论是 MAC、Window、Linux 还是其他任何机器,只需指定要运行的端口即可。 In your package.json do this :在你的 package.json 中这样做:

 "start": "export PORT=3001 && react-scripts start "

npm start --port 8000 does not work. npm start --port 8000不起作用。 You should write command like this.你应该这样写命令。

ng serve --port 8000

$npm run start -- -p 8000 $npm 运行开始 -- -p 8000

8000 is port number you can change your port number 8000 是端口号,您可以更改端口号

(For those still suffering despite trying all the answers.) (对于那些尽管尝试了所有答案仍然受苦的人。)

Here is a quick fix that should not be used in production!这是一个不应该在生产中使用的快速修复!

Just forward the source port to the target one that you need.只需将源端口转发到您需要的目标端口即可。 Open another shell and tap this command:打开另一个 shell 并点击此命令:

socat tcp-listen:8000,reuseaddr,fork tcp:192.168.1.48:3000

In this example;在这个例子中; 3000 is the port of the server and 8000 the port you want to forward to (or receive from to be precise) 3000 是服务器的端口,而 8000 是您要转发到的端口(或者更准确地说是从中接收)

Enjoy!享受!

npm start -- --port "port number"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM