This article focuses on the method of creating the first application for node. js, as well as the introduction of five different scenarios. So that everyone can learn something, learn this, and then let's come and see this article.
First, let's try to create the first application:
Before we create the first "Hello, world!" app for node. JS, let's start by understanding which parts of the node. JS application are composed:
1. Introduction of the required module: we can use the Require directive to load the node. JS module.
2, create the server: The server can listen to the client's request, similar to Apache, Nginx and other HTTP server.
3, receive requests and response requests, the server is easy to create, after the client can use the browser or terminal to send HTTP requests, the server receives the request to return the response data
Step one, introduce required module
We use the Require directive to load the HTTP module and assign the instantiated HTTP value to the variable HTTP, as shown in the following example:
var http = require ("http");
Step two, create the server
Next we use the Http.createserver () method to create the server and bind port 8888 using the Listen method. The function receives and responds to data through the request, response parameter.
As an example, create a file called Server.js in the root directory of your project and write the following code:
var http = require (' http '); Http.createserver (function (request, response) { //Send HTTP Header //HTTP status value: 200:ok
//content Type: Text/plain response.writehead ($, {' Content-type ': ' Text/plain '}); Send the response data "Hello World" response.end (' Hello world\n ');}). Listen (8888);//terminal print the following information Console.log (' Server running at http://127.0.0.1:8888/');
Parsing the HTTP Server for node. JS:
The first line requests (require) node. js's own HTTP module and assigns it to the HTTP variable.
Next we invoke the function provided by the HTTP module: Createserver. This function returns an object that has a method called Listen, which has a numeric parameter that specifies the port number that the HTTP server listens on.
The above code we completed a working HTTP server, that is, the above code we can complete a local server build, when we run the above code, a local server began to run, we only need to open the browser to this server to send a request, This server will return your defined return data to the browser (to learn more about node. js, go to topic.alibabacloud.comnode.js Chinese Reference manual section)
Use the node command to execute the above code:
Node Server.jsserver running at http://127.0.0.1:8888//cmd output information
Next, open the browser to access http://127.0.0.1:8888/, and you'll see a page with "Hello World".
Nodejs-helloworld
Now let's look at another five scenarios for node. JS:
First of all, I would like to tell you why call another five kinds of scenes, because the small part in front already said 10 kinds of, everyone is interested to see this article: What is node. js? 10 application scenarios for node. js
1. Browser Environment tool: Browserify
The advent of browserify allows the Nodejs module to run in the browser, using the syntax format of the require () to organize the front-end code and load NPM modules. In the browser, call the Browserify compiled code, also written in the <script> tag.
The operation of Browserify is divided into 3 steps. 1. Write node program or module, 2. Pre-compiled with browserify into Bundle.js, 3. Load the bundle.js in the HTML page.
2. Command-line programming tool: Commander
The commander is a lightweight Nodejs module that provides user command line input and parameter parsing power. Commander originates from a Ruby project with the same name. Commander Features: Self-documenting code, auto-Generate help, combine short parameters ("ABC" = = "-a-b-c"), default options, mandatory options, command parsing, prompt.
3.Web Console tool: Tty.js
Tty.js is a command-line window that supports running in a browser, based on the node. JS platform and relies on the Socket.io library to communicate with the Linux system through WebSocket. Features: Support Multi-tab window model; Support VIM,MC,IRSSI,VIFM syntax; Support Xterm mouse events; Support 265 color display; Support session.
4. Client application tool: Node-webkit
Node-webkit is a fusion of Nodejs and Webkit technologies, providing an underlying framework for client application development across Windows, Linux platforms, Use popular web technologies (NODE.JS,JAVASCRIPT,HTML5) to write your application's platform. Application developers can easily use web technologies to implement a variety of applications. Node-webkit performance and features have made it the world's leading web technology application platform.
5. Operating system: Node-os
Nodeos is a friendly operating system developed with Nodejs, which is built entirely on top of the Linux kernel, and uses shell and NPM for package management, using Nodejs not only to manage packages well, but also to manage scripts, interfaces, and so on. Currently, both Docker and vagrant are built with the first version of Nodeos.
The above is the creation of the first application of node. JS and the other five scenarios (want to learn more relevant knowledge, to Topic.alibabacloud.comnode.js Video Tutorial column learning), problematic can be asked below