Use q. js in node. js to implement api promise
Here we will look at how to use q. js to implement node api promise.
I. Everything starts with install
The Code is as follows:
Npm install q
Ii. Standard node style api promise Me
lable end. Output lable:100ms (can calculate intermediate processing time) console.timeend (' lable '); Prints information about the current stack lable. Output trace:lable---------------------- console.trace (' lable '); Assertion syntax: Console.assert (Expression,[message]) //output assertionerror: Abnormal Console.assert (false, ' abnormal slightly ');V. BufferThe appearance of this buffer is basically to solve the situation that JS itself is unfriendly to the binary syste
The default node project has only one master process, which is managed by the cluster API, and a worker is added to each core, called the branching process. Example var cluster = require (' cluster '); var http = require (' http ');//Gets the number of CPU cores of the server var Numcpus = require (' OS '). CPUs (). Length;if ( Cluster.ismaster) { for (var i = 0; i The Cluster
1. Install node, NPM2. Installing StrongloopNPM install-g--unsafe-perm Install Strongloop3. Create a working directory and configure the loopback appmkdir Todos CD TODOSSLC loopbackThe installation will appear successfully Change the directory to your application $ cd yourrestful in the application to create the model $ SLC Loopback:model Run application $ node.4. Install MySQLInstall loopback-
An important task of node is to create Web Services. Next we will learn an important API related to this-HTTP. We use HTTP. createserver () to create an HTTP service instance to process requests from customers. The HTTP module contains some important content. Let's take a look at it.
HTTP Server
Let's first look at a simple example of how to create a simple server.
Require ('http'). createserver (F
"]==data["id"]) {Con[i]=data; Flag=true; Break; }} result.msg= "The modification failed, the customer needs to be modified does not exist"; if(flag) {Fs.writefilesync (Custompath,json.stringify (con),"Utf-8"); Result={code:0, msg:"Modified successfully"}} res.writehead (200,{' content-type ': ' Application/json;charset=utf-8; '}); Res.end (json.stringify (result)); }) return; } //If the requested address is not one of these, the prompt does not existRes.writehead (404,{' conten
The two modules to be introduced today are assert and VM. Next we will introduce them separately.
Use assert for testing
Node, we can use the assert module to test the code. Equal () and notequal () are used to determine equality and inequality respectively. The first parameter is the expected value, the second parameter is the real value, and the third parameter is the exception information. The example is as follows:
var assert = require('assert')
(' buf concat '); var list = [];var len = 0;for (var j=0; jThe latter must be fast
At this point, I was out of my mind, trying to compare the second method of a 1 with the second of 3, which is faster:var buf = new buffer (' SASDASD '), Console.time (' Write 1024*1024*10 buffer '), var buffer3 = new buffer (700000); for (var j=0; j lt;100000; J + +) { var x = ' sasdasd '; Buffer3.write (x, j);} Console.log (' buffer3.length: ' +buffer3.length); Console.timeend (' Write 1024*1024*10 buf
:...True2. Invocation of functionsThis points to the global object in the execution environment (browser->window nodejs->global)function Pet2 (words) {This.words = wordsConsole.log (This.words)Console.log (This===global)}Pet2 (' ... ')Printing results:...True3. ConstructorsThis method is called by the instance object, which points to this instance objectfunction PEt3 (words) {This.words = wordsThis.speak = function () {Console.log (This.words)Console.log (This)}}var cat = new PEt3 (' Miao ')Prin
RESTRepresentational state Transfer is a set of schema constraints and principles. Applications or designs that meet these constraints and principles are restful.RESTful Web ServicesWeb Service is a platform-independent, low-coupling, self-contained, programmable Web-based application that uses Open XML (a subset of standard common markup languages) to describe, publish, discover, orchestrate, and configure these applications for the development of distributed, interoperable applications.RESTful
triggeredEvent Pipe flow to the source stream of this writable stream occurs when the pipe () method of the readable stream is called and adds this writable stream as its target;Writer.on (' pipe ',function(src) { console.log (' Something is being channeled to writer ');})Event UnpipeClass stream. DuplexFor example, TCP nested word zlib stream Crypto streamClass stream. Transform is a duplex output computed by the input, with both readable and writable interfacessuch as zlib flow, crypto flo
This article is published in my blog.The previous note says to create a buffer instance, this section continues with buffer. This section explains some of the static methods, writes, and read methods for buffer.Buffer.isencoding (encoding) to determine whether Nodejs supports this encoding Console.log (buffer.isencoding ("gb2312"));//No matter how node does not support this encoding, Rote Console.log (buffer.isencoding ("UTF8")); True Consol
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.