Nodejs + socket. io initial experience

Source: Internet
Author: User
Tags install node node server

First, install node.jsalong the nextpath. You can also customize it to add the directory of node.exe to the system environment variable.

Create a project: d:/project

Cd d:/project

Then run npm install socket. io to see the newly produced node_module directory under this directory,

Set up server. js as follows:

var io = require('socket.io').listen(8080);io.sockets.on('connection', function (socket) {  socket.emit('news', { hello: 'world' });  socket.on('my other event', function (data) {    console.log(data);  });});
Then, create the index.html file under Directory: D: \ project \ node_modules \ socket. io \ lib.

The content is as follows:

   
   
   
   
   
   Test nodejs  <Script src = "http: // localhost: 8080/socket. io/socket. io. js "> </script> <script> var socket = io. connect ('HTTP: // localhost: 100'); socket. on ('News', function (data) {console. log (data); socket. emit ('My other event', {my: 'data'}) ;}); </script>
Test the use of node server. js and then click the javasindex.html file to open the Chrome review element ..

Set global node_module. Create a new node-global File under the node installation directory to modify the npmrc file under npm.

Prefix = D: \ software \ node-global

Run

Npm config set prefix "D: \ software \ node-global"
Npm config set cache "D: \ software \ node-cache"

Add node_global to NODE_PATH and PATH

Then you can run server. js globally.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.