如何安装 Node.js、npm、socket.io 并使用它们?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18990494/
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 15:44:11  来源:igfitidea点击:

How to install Node.js, npm, socket.io and use them?

node.jssocket.ionpm

提问by Pars

I am newbie to Node.js
Can someone explain me how can I install Node.js, npm and socket.io step by step.

我是 Node.js 的新手
有人可以解释我如何逐步安装 Node.js、npm 和 socket.io。

Thanks.

谢谢。

回答by Krasimir

1.. Go to http://nodejs.organd click on Install button

1.. 前往http://nodejs.org并点击安装按钮

2.. Download node and install it

2.. 下载节点并安装

3.. Create an empty folder on your hard disk

3.. 在你的硬盘上创建一个空文件夹

4.. Create an package.json file with the following content

4.. 创建一个 package.json 文件,内容如下

{
    "name": "App",
    "version": "0.0.1",
    "description": "App",
    "dependencies": {
        "socket.io": "latest"
    },
    "author": "developer"
}

5.. Open windows's command prompt (press Windows key + R and type cmd)

5.. 打开 Windows 的命令提示符(按 Windows 键 + R 并键入cmd

6.. Navigate to your newly created directory with cdcommand

6.. 使用cd命令导航到您新创建的目录

7.. Type npm installin that directory

7..在该目录中输入npm install

8.. Wait till everything is downloaded and installed

8.. 等到所有东西都下载并安装好

9.. Create a file app.js with the following content:

9.. 创建一个 app.js 文件,内容如下:

var app = require('http').createServer(handler)
  , io = require('socket.io').listen(app)
  , fs = require('fs')

app.listen(3000);

function handler (req, res) {
  fs.readFile(__dirname + '/index.html',
  function (err, data) {
    if (err) {
      res.writeHead(500);
      return res.end('Error loading index.html');
    }

    res.writeHead(200);
    res.end(data);
  });
}

io.sockets.on('connection', function (socket) {
  socket.emit('news', { hello: 'world' });
  socket.on('my other event', function (data) {
    console.log(data);
  });
});

10.. Create a file index.html with the following content

10..创建一个文件index.html,内容如下

<script src="/socket.io/socket.io.js"></script>
<script>
  var socket = io.connect('http://localhost');
  socket.on('news', function (data) {
    console.log(data);
    socket.emit('my other event', { my: 'data' });
  });
</script>

11.. Again, go to the command prompt (console) and type node app.js. This will run nodejs server and you may open localhost:3000

11.. 再次进入命令提示符(控制台)并输入node app.js。这将运行 nodejs 服务器,您可以打开localhost:3000

回答by shmuli

1. Install Node.js and NPM (Node Package Manager) on your local machine.

1. 在本地机器上安装 Node.js 和 NPM(节点包管理器)。

Windows installers are available at http://www.nodejs.org/. Simply download the relevant installer, and double-click it to get it working on your machine. You can verify that node is correctly installed by double-clicking the node.exe file in the installation directory, and running any Javascript commands. If you can type in "1+1" and get the resulting "2", then Node is running properly.

Windows 安装程序可从http://www.nodejs.org/ 获得。只需下载相关的安装程序,然后双击它即可在您的机器上运行。您可以通过双击安装目录中的 node.exe 文件并运行任何 Javascript 命令来验证节点是否已正确安装。如果您可以输入“1+1”并得到结果“2”,则 Node 运行正常。

Since you installed Node using an installer, NPM is already installed. If you compiled Node from source installation however, then you'll have to install NPM separately. You can find instructions for that at http://www.npmjs.org/.

由于您使用安装程序安装了 Node,因此 NPM 已经安装。但是,如果您从源代码安装编译 Node,那么您必须单独安装 NPM。您可以在http://www.npmjs.org/ 上找到相关说明。

If your NPM is correctly installed you'll get the following output when you type npmin command prompt from your root directory:

如果您的 NPM 安装正确,当您npm从根目录输入命令提示符时,您将获得以下输出:

where <command> is one of:
    add-user, adduser, apihelp, author, bin, bugs, c, cache,
    completion, config, ddp, dedupe, deprecate, docs, edit,
    explore, faq, find, find-dupes, get, help, help-search,
    home, i, info, init, install, isntall, la, link, list, ll,
    ln, login, ls, outdated, owner, pack, prefix, prune,
    publish, r, rb, rebuild, remove, restart, rm, root,
    run-script, s, se, search, set, show, shrinkwrap, star,
    start, stop, submodule, tag, test, tst, un, uninstall,
    unlink, unpublish, unstar, up, update, version, view,
    whoami

npm <cmd> -h     quick help on <cmd>
npm -l           display full usage info
npm faq          commonly asked questions
npm help <term>  search for help on <term>
npm help npm     involved overview

2. Run the install package.

2. 运行安装包。

Now that you've successfully setup Node and NPM, you can run the install command that you found on the socket.io website. Simply make sure that you're running NPM from the command line. That will download and install the package on your local machine.

现在您已成功设置 Node 和 NPM,您可以运行在 socket.io 网站上找到的安装命令。只需确保您从命令行运行 NPM。这将在您的本地计算机上下载并安装该软件包。

回答by Moritz Mahringer

You have to install Node.js, npm is their package manager (Node Package Manager).

你必须安装Node.js, npm 是他们的包管理器(Node Package Manager)。

Edit: If you don't understand Node.js here is a quick overview:

编辑:如果您不了解 Node.js,这里是一个快速概述:

  1. Create a text file ("app.js") and install the dependencies ("npm install socket.io")
  2. Paste the code from socket.io
  3. save and run (in the folder: "node app.js")
  1. 创建一个文本文件(“app.js”)并安装依赖项(“npm install socket.io”)
  2. 从 socket.io 粘贴代码
  3. 保存并运行(在文件夹中:“node app.js”)