反应教程 - 如何为reactJs应用程序启动节点服务器?

Mar*_*arc 18 localhost npm reactjs server

我刚刚开始使用react.js教程,我已经下载了文件,然后提到:

"通过在浏览器中打开http:// localhost:3000来跟踪您的进度(启动服务器后)."

我知道这可能听起来很愚蠢,(因为我是React的初学者,请耐心等待)但是如何在这个实例中启动服务器?

谢谢.

Car*_*llo 12

npm start来自项目根目录的非常可靠的机会.

正确打包的模块将配置一些节点脚本package.json.这是习惯使用start的脚本运行的开发环境,虽然有些可能会使用build,dev或其他名称.


w41*_*1 3 7

下面是官方安装过程:链接,以及官方推荐教程

# install react cli
npm install -g create-react-app

# create app
create-react-app my-react-app-name

# go to project folder
cd my-react-app-name

# install dependencies
npm install

# start live server
npm start
Run Code Online (Sandbox Code Playgroud)

输出:

$ You can now view my-react-app-name in the browser.

$ Local:            http://localhost:3000/
$ On Your Network:  http://192.168.0.105:3000/

$ Note that the development build is not optimized.
$ To create a production build, use npm build.
Run Code Online (Sandbox Code Playgroud)


RBT*_*RBT 6

您可以运行以下任一命令来启动 ReactJS 应用程序的节点服务器:

  1. npm 运行脚本启动
  2. npm 运行开始
  3. npm启动

上述所有命令都是等效的,但人们更喜欢第三个命令,因为它在键盘上输入的时间最短。

这些命令中的 start 参数映射到任何ReactJS 应用程序的 package.json 文件中脚本配置下的启动键以下是我的 hello-world 应用程序的示例package.json文件:

{
  "name": "hello-world",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "react": "^15.4.2",
    "react-dom": "^15.4.2"
  },
  "devDependencies": {
    "react-scripts": "0.9.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}
Run Code Online (Sandbox Code Playgroud)

可以看到startreact-scripts start键前面写着。因此,当我们运行我在开始时列出的三个命令中的任何一个时,命令将被触发,例如。react-scripts startnpm start