nodejs where to start?

后端 未结 4 1315
臣服心动
臣服心动 2021-02-01 23:21

I\'ve installed nodejs and ran couple of simple examples like opening a server on a port and listen on that port.

However, I still can not relate nodejs to web develop

相关标签:
4条回答
  • 2021-02-01 23:34

    First Just try to implement basic application and get a feel of the framework. There are several tutorials online for example:

    http://net.tutsplus.com/tutorials/javascript-ajax/learning-serverside-javascript-with-node-js/

    Documentation at http://nodejs.org/

    http://howtonode.org/

    Also there is a 70 min screencast by peepcode which costs 9$

    https://peepcode.com/products/nodejs-i

    There are also several apps on github, which you take a look at. Reading code is always the best.

    0 讨论(0)
  • 2021-02-01 23:37

    I find hexes example helpful as reference for a realtime game using nodejs.

    https://github.com/joyent/node-camp/tree/master/hexes

    0 讨论(0)
  • 2021-02-01 23:42

    I found The Node Beginner Book helpful. It's probably too basic for what you're looking to do, however.

    0 讨论(0)
  • 2021-02-01 23:48

    I'd recommend looking at the project Socket.IO and Socket.IO-node. It uses HTML5 WebSockets if available, and falls back automatically and gracefully (no intervention required) to Flash sockets and XHR-polling as necessary

    Here's a script to download the files:

    mkdir socket.io
    cd socket.io
    git clone https://github.com/LearnBoost/Socket.IO.git --recursive
    git clone https://github.com/LearnBoost/Socket.IO-node.git --recursive
    

    Here's the server.js file:

    var http=require('http');
    var url=require('url');
    var fs=require('fs');
    var sys=require('sys');
    var io=require('./socket.io/Socket.IO-node');   //adjust path as necessary...
    
    var server=http.createServer(function(req,res){
        res.writeHead(200,{'Content-Type':'text/html'});
        res.write('Hello world');
        res.end();
    });
    server.listen(8000);
    
    var socket=io.listen(server);
    
    socket.on('connection', function(client){
      onConnection(client);
      client.on('message', function(){
        onMessage();
      })
      client.on('disconnect', function(){
        onDisconnect();
      })
    });
    function onConnection(client){
      console.log('connection');
      //client.connected;   //tests if connected
      //client.send("message");
      //client.broadcast("message");   //send to all other conns
    }
    function onMessage(){
      console.log('message');
    }
    function onDisconnect(){
      console.log('disconnect');
    }
    
    });
    

    Run the above server with sudo node server.js

    And here is your index.html to be run in a browser:

    <script src="./socket.io/Socket.IO/socket.io.js" type="text/javascript" charset="utf-8"></script>   <!--Adjust path as necessary-->
    <script>
    var host="localhost";
    var port=8000;
    
    var socket=new io.Socket(host,{'port':port});
    
    socket.connect();
    socket.on('connect',function(){onConnect();}) 
    socket.on('message',function(data){onMessage(data);}) 
    socket.on('disconnect',function(){onDisconnect();});
    
    function onConnect(){
      ///alert('connect');
    }
    function onMessage(data){
      //alert('message');
    }
    function onDisconnect(){
      //alert('disconnect');
      socket.connect();
    }
    
    </script>
    
    0 讨论(0)
提交回复
热议问题