I am newbie to Node.js
Can someone explain me how can I install Node.js, npm and socket.io step by step.
Thanks.
I am newbie to Node.js
Can someone explain me how can I install Node.js, npm and socket.io step by step.
Thanks.
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
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.
You have to install Node.js, npm is their package manager (Node Package Manager).
Edit: If you don't understand Node.js here is a quick overview: