Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Thanks.

like image 386
Pars Avatar asked Sep 24 '13 19:09

Pars


People also ask

How do I reinstall Node.js and npm?

b) Reinstalling using a Node installer Using the official Node installer is the easiest way to reinstall Node. js and npm on your Windows environment. To use this option, you can go to the Node. js download page and reinstall the latest Node.

Does installing Node.js automatically install npm?

The Node. js installer carries the Node. js core file, and, consequently, the installation process installs both Node. js and npm from the installer file.

What is Socket.IO npm?

Socket.IO is a library that enables low-latency, bidirectional and event-based communication between a client and a server. It is built on top of the WebSocket protocol and provides additional guarantees like fallback to HTTP long-polling or automatic reconnection.


2 Answers

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

2.. Download node and install it

3.. Create an empty folder on your hard disk

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

{
    "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)

6.. Navigate to your newly created directory with cd command

7.. Type npm install in that directory

8.. Wait till everything is downloaded and installed

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

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

<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

like image 99
Krasimir Avatar answered Sep 19 '22 20:09

Krasimir


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

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.

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/.

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

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.

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.

like image 43
shmuli Avatar answered Sep 20 '22 20:09

shmuli