Home  >  Article  >  Web Front-end  >  How to create the first application in node.js? Five application scenarios of node.js

How to create the first application in node.js? Five application scenarios of node.js

寻∝梦
寻∝梦Original
2018-09-05 15:12:271670browse

This article mainly introduces the method of creating the first application in node.js, and also introduces five application scenarios. So that everyone can learn something and learn these, let’s read this article together

First let’s try to create the first application:

Before we create the first "Hello, World!" application of Node.js, let us first understand what parts the Node.js application consists of:

1. Introduce the required module: We Node.js modules can be loaded using the require directive.

2. Create a server: The server can monitor client requests, similar to HTTP servers such as Apache and Nginx.

3. Receive requests and respond to requests. The server is easy to create. After the creation is successful, the client can use a browser or terminal to send an HTTP request. The server returns the response data after receiving the request.

Step 1. Introduce the required module

We use the require instruction to load the http module and assign the instantiated HTTP value to the variable http. The example is as follows:

var http = require(“http”);

Step 2. Create the server

Next we use the http.createServer() method to create the server and use the listen method to bind the 8888 port. Functions receive and respond to data through request and response parameters.

The example is as follows. 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) {
    // 发送 HTTP 头部 
    // HTTP 状态值: 200 : OK
    // 内容类型: text/plain
    response.writeHead(200, {'Content-Type': 'text/plain'});
    // 发送响应数据 "Hello World"
    response.end('Hello World\n');
}).listen(8888);
// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');

Analyze Node. js HTTP server:

The first line requests the http module that comes with Node.js and assigns it to the http variable.

Next we call the function provided by the http module: createServer. This function will return an object. This object has a method called listen. This method has a numeric parameter that specifies the port number that the HTTP server is listening on.

With the above code, we have completed a working HTTP server. In other words, with the above code, we can complete the construction of a local server. When we run the above code, a local server will start running. , we only need to open the browser and send a request to this server, and the server will return the return data you defined to the browser (if you want to learn more about node.js, go to the PHP Chinese websitenode.js中文Refer to the manual column to learn)

Use the node command to execute the above code:

node server.js
Server running at http://127.0.0.1:8888 //cmd输出信息

Next, open the browser and visit http://127.0.0.1: 8888/, you will see a web page that says "Hello World".

nodejs-helloworld

Now let’s take a look at the other five application scenarios of node.js:

Let me start by telling you Why are they called the other five scenarios? Because the editor has already mentioned ten scenarios before. If you are interested, you can read this article: What does node.js do? Introduction to 10 application scenarios of node.js

1. Browser environment tools: browserify

The emergence of Browserify allows Nodejs modules to run in the browser , use the require() syntax format to organize the front-end code and load the npm module. In the browser, the code compiled by calling browserify is also written in the <script> tag. </script>

The operation of using Browserify is divided into 3 steps. 1. Write a node program or module, 2. Use Browserify to precompile it into bundle.js, 3. Load bundle.js in the HTML page.

2. Command line programming tool: Commander

commander is a lightweight nodejs module that provides powerful functions for user command line input and parameter parsing. Commander originates from a Ruby project of the same name. Features of commander: self-recording code, automatically generating help, merging short parameters ("ABC" == "-A-B-C"), default options, mandatory options, command parsing, and prompts.

3. Web console tool: tty.js

tty.js is a command line window that supports running in the browser, based on the node.js platform. Depends on the socket.io library to communicate with the Linux system through websocket. Features: supports multi-tab window model; supports vim, mc, irssi, vifm syntax; supports xterm mouse events; supports 265-color display; supports session.

How to create the first application in node.js? Five application scenarios of node.js

4. Client application tool: node-webkit

Node-Webkit is a fusion of NodeJS and WebKit technology, providing a The underlying framework for client application development across Windows and Linux platforms, and a platform for writing applications using popular web technologies (Node.JS, JavaScript, HTML5). Application developers can easily leverage Web technologies to implement various applications. The performance and features of Node-Webkit have made it the world's leading web technology application platform.

5. Operating system: node-os

NodeOS is a friendly operating system developed using NodeJS. The operating system is completely built on the Linux kernel and uses shell and NPM for package management. Using NodeJS can not only perform package management well, but also Can manage scripts, interfaces, etc. very well. Currently, both Docker and Vagrant are built with the first version of NodeOS.

How to create the first application in node.js? Five application scenarios of node.js

The above is the first application and five other application scenarios for creating node.js in this article (if you want to learn more related knowledge, go to PHP Chinese Learn from the node.js video tutorial column on the Internet), if you have any questions, you can ask below

[Editor’s recommendation]

How to set up the html search box? Examples of how to use the input tag in the html search box

html How to use the base tag? Summary of usage of html base tag

The above is the detailed content of How to create the first application in node.js? Five application scenarios of node.js. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn