A simple example of Nodejs+express+socket.io

Source: Internet
Author: User
Tags emit

Keyword: Nodejs,express,socket.io.

Os:windows 8.1 with Update Pro.

1. Install the nodejs:http://nodejs.org/.

2. Initialize a Nodejs web app:

Open the cmd window.

Run cmd: "mkdir myapp", create a new folder named MyApp.

Run cmd: "CD MyApp", switch to the folder MyApp.

Run cmd: "NPM init", create file Package.json.

3.Express Getting Started app:

In the cmd window above, run cmd: "NPM install Express--save", install "Express" Node_module, "--save" means save Express to Package.json.

Add the file App.js to the MyApp folder, assuming that Package.json inside the main is app.js.

// App.js var app = require (' Express ') (); var http = require (' http '). Server (app); App.get (function(req, res) {  res.send (' );}); Http.listen (function() {  console.log (' listening on *:3000 ');});

Run cmd: "Node App", start MyApp.

Open "Http://localhost:3000/" in Chrome and show "Hello World".

Enter "CTRL + C" in the CMD window to terminate the operation of MyApp.

4.socket.io Getting Started App

Run in the cmd window above: "NPM install--save Socket.io".

Add index.html to the MyApp folder with the following contents:

<!DOCTYPE HTML><HTML>  <Head>    <title>Socket.io Chat</title>    <style>      * {margin:0;padding:0;box-sizing:Border-box; }Body{Font:13px Helvetica, Arial; }form{background:#000;padding:3px;position:fixed;Bottom:0;width:100%; }Form Input{Border:0;padding:10px;width:90%;Margin-right:. 5%; }Form Button{width:9%;background:RGB (224, 255);Border:None;padding:10px; }#messages{List-style-type:None;margin:0;padding:0; }#messages Li{padding:5px 10px; }#messages li:nth-child (odd){background:#eee; }    </style>  </Head>      <Body>      <ulID= "Messages"></ul>    <formAction="">      <inputID= "M"AutoComplete= "Off" /><Button>Send</Button>    </form>          <Scriptsrc= "/socket.io/socket.io.js"></Script>    <Scriptsrc= "Http://code.jquery.com/jquery-1.11.1.js"></Script>    <Script>      varSocket=io (); $('form'). Submit (function() {Socket.emit ('Chat Message', $('#m'). Val ()); $('#m'). Val ("'); return false;          }); Socket.on ('Chat Message', function(msg) {$ ('#messages'). Append ($ ('<li>'). Text (msg));    }); </Script>  </Body></HTML>

Modify the App.js as follows:

//App.jsvarApp = require (' Express ')();varHTTP = require (' http '). Server (app);varIO = require (' Socket.io ') (HTTP); App.get (‘/‘,function(req, res) {Res.sendfile (' Index.html ');}); Io.on (' Connection ',function(socket) {Console.log (' A user connected '); Socket.on (' Disconnect ',function() {Console.log (' User disconnected ');      }); Socket.on (' Chat message ',function(msg) {Console.log (' Message: ' +msg); Io.emit (' Chat message ', MSG); });}); App.set (' Port ', Process.env.PORT | | 3000);varServer = Http.listen (app.get (' Port '),function() {Console.log (' Start at Port: ' +server.address (). port);});

Run "node index" again and use Chrome to open the " http://localhost:3000/", shown below"

Reference:

nodejs:http://nodejs.org/

expressjs:http://expressjs.com/

socket.io:http://socket.io/

A simple example of Nodejs+express+socket.io

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.