Node.js+express+websocket installation configuration under Windows

Source: Internet
Author: User
Tags emit node server using git

node. JS is a JavaScript run environment (runtime). It actually encapsulates the Google V8 engine. The V8 engine executes JavaScript very fast and performs very well. node. JS optimizes some of the special use cases, providing an alternative API that allows V8 to run better in a non-browser environment. node. JS is a platform built on the chrome JavaScript runtime to easily build responsive, easy-to-scale Web applications. node. js is lightweight and efficient with event-driven, non-blocking I/O models and is ideal for data-intensive, real-time applications running on distributed devices.

Express is a minimalist, flexible Web application Development framework based on the node. JS platform that provides a range of powerful features to help you create a wide variety of web and mobile device applications. Express is also the officially recommended web framework.

  Here's how to run a Web site in the window environment:

1 installing node. js

2 Create a new folder Nodejs_express

3 Open cmd, navigate to Nodejs_express

Copy CodeThe code is as follows:
E:\myapp\nodejs_express

4 Project Initialize NPM init, after the configuration is completed, a file is generated under the folder:

Package.json

5 Installing EXPRESS:NPM Install Express, after successful installation, a node_modules directory will be generated under the folder

6 Express Project Initialization Express (express Hello)

7 Installing the Dependency package NPM install Serve-favicon Morgan cookie-parser body-parser path;>npm Install Jade

What's more simple:

Enter the project directory, install the Nodejs dependency module NPM install (CD hello; NPM install)

8 start NPM Start and access on the 3000 port of the local IP

9 port number can be modified in the Bin/www file

The above is a small series to introduce you to the node.js+express configuration of the relevant knowledge of the tutorial, I hope that we have some help, if you have any questions please give me a message, small series will promptly reply to you. Thank you very much for the support of the Scripting House website!

node. JS installation under Windows platform

Go directly to Nodejs's official website http://nodejs.org/Download Nodejs installation program, double-click on the installation can be

To test whether the installation was successful:

At the command line input node–v should be able to view the Nodejs version number of the current installation

A simple example
Write a short code, save it as helloworld.js, and see how Nodejs is used.

As follows: The code is primarily to create an HTTP server.

[JavaScript]View PlainCopy
    1. var http = require ("http");
    2. Http.createserver (function (request, response) {
    3. Response.writehead ($, {"Content-type": "text/html"});
    4. Response.Write ("Hello world!");
    5. Response.End ();
    6. }). Listen (8080);
    7. Console.log ("Server running at http://localhost:8080/");

Open the command line and go to the path where the current file is stored and run the Node helloworld.js command

If everything works, you can see the command line output: Server running at http://localhost:8080/

At the same time, in the browser input http://localhost:8080/, you can see a page written HelloWorld.



Installing NPM

NPM has a lot of good nodejs packages to solve common problems, such as using Node-mysql, you can easily link to MySQL through the Nodejs, database operation

In the development process will often need to use other packages, using NPM can download these packages for the program to call

A) If the system does not have Git installed, you can download the files required by NPM directly to HTTPS://GITHUB.COM/ISAACS/NPM.

b) If you have git, you can download it using Git.

[Plain]View PlainCopy
    1. git clone--recursive git://github.com/isaacs/npm.git

After downloading to the NPM file, the command line first goes to the NPM location and enters the following code to install it.

[Plain]View PlainCopy
    1. Node Cli.js install NPM-GF

Install Express

Express is a common framework for NODEJS.

A) Global installation

[Plain]View PlainCopy
    1. NPM Install EXPRESS-GD

b) installed under the current folder

[Plain]View PlainCopy
    1. NPM Install Express

After the installation is successful, the command line prompts NPM info OK

-G represents the installation to the Lib in Node_path, and the-D represents the installation of the dependency kit. If you do not have-G, the current directory will be installed (a Node_modules folder will be created).

Referencing Express packages in projects

Cases:

[JavaScript]View PlainCopy
    1. var express = require (' express ');
    2. var app = Module.exports = Express.createserver ();

If you do not have Express installed, you first need to install an express in the current project folder

After the command line goes to the current path, run

[Plain]View PlainCopy
    1. NPM Install Express

Once the installation is complete, you can see a "node_modules" folder in the current directory with an "Express" folder

Note: Packages referenced in the project will be installed in the "Node_modules" folder

Create a project with Express

Enter "Express project name" on the command line to create a new project under the current folder

The following files are included:

With this method, just create an empty project framework, and a simple instance program, run App.js can view (also need to install the Jade package in the project file directory, method similar to install Express)

Reference website:

node. JS Foundation Http://www.infoq.com/cn/master-nodejs

node. js Getting Started Tutorial http://nodebeginner.org/index-zh-cn.html

node. JS Chinese Document Http://cnodejs.org/cman/index.html

Express Document Http://expressjs.com/guide.html#routing

Cnode Community http://club.cnodejs.org/

Common operations with Command line:

1) After the file is run, bring up the command input field again with CTRL + C
2) go to a disk under F:
3) Go to a folder under CD F:\nodeExamples
4) go back to the parent directory CD.

    1. Download the installation file from node website, address: http://nodejs.org/

    2. This installer is also very regular, order click Next can be, or do some simple choice, needless to say, just a little more emphasis, one step: Add to Path must be selected. The function of this is simply to add a node-related command to the Windows command line, please Baidu for details.

    3. We were very happy to have node installed, and then install Express,express is the only web framework that node officially recommends, providing a lot of basic convenience features.

    4. 4

      Execute "npm install-g Express" on the command line waiting to download and complete the installation automatically. One way to test Express installation is to look at its version number and execute the command "express-v" to output the version number normally, but in the event of an abnormal condition, "Express is not an internal or external command " So the question comes, what's the reason?

    5. 5

      There are two possibilities: ① installing node in the second step does not add an environment variable, which can be solved by adding the environment variable to node. ②express 4.x version of the command tool, you need to install a command tool, execute the command "npm install-g express-generator" After the completion of the test.

Nodejs implementation of the WebSocket 4 ways: Socket.io, Websocket-node, Faye-websocket-node, Node-websocket-server, the main use of this is Socket.io

1, the service side:

1) First install the Socket.io

NPM Install Socket.io

2) Server.js

[JavaScript]View PlainCopy
  1. var app = require (' http '). Createserver (handler),
  2. IO = require (' Socket.io '). Listen (APP),
  3. FS = require (' FS ')
  4. App.listen (8080);
  5. Io.set (' Log level ', 1); To turn off debug information in the Socket.io
  6. function handler (req, res) {
  7. Fs.readfile (__dirname + '/index.html ',function (err, data) {
  8. if (err) {
  9. Res.writehead (500);
  10. return Res.end (' Error loading index.html ');
  11. }
  12. Res.writehead ($, {' content-type ': ' text/html '});
  13. Res.end (data);
  14. });
  15. }
  16. Io.sockets.on (' connection ', function (socket) {
  17. Socket.emit (' news ', {hello: ' world ');
  18. Socket.on (' My other event ', function (data) {
  19. Console.log (data);
  20. });
  21. });


2. Client:

1) WebSocket is the HTML5 standard, the browser has been supported, its programming interface roughly connect, close, open, send several interfaces, if you want to use the browser native way to write websocket, more cumbersome, So you can download a client library for easy programming, using the Socket.io client library, click the Open link

2) index.html

[HTML]View PlainCopy
  1. <! DOCTYPE HTML>
  2. <HTML lang="en">
  3. <head>
  4. <meta http-equiv= "content-type" content= "text/html; Charset=utf-8 " />
  5. <title>ssocket</title>
  6. <script type="Text/javascript" src="Https://cdn.socket.io/socket.io-1.3.5.js"> </Script>
  7. </head>
  8. <body>
  9. <script type="Text/javascript">
  10. var socket = io.connect (' http://localhost:8080 ');
  11. Socket.on (' News ', function (data) {
  12. alert (Data.hello);
  13. Socket.emit (' My other event ', {my: ' Data '});
  14. });
  15. </Script>
  16. </body>
  17. </html>


3. Test:

Start Server Nodejs Code: node Server.js

In the browser input http://localhost:8080/index.html


Browser Print out: World


Command line print out: {my: ' Data '}

Updating

Node.js+express+websocket installation configuration under Windows

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.