In node. js, q. js is used to implement api promise. node. jsq. js
About what is promise and promise solution is what problem, please experience node callback asynchronous encoding method, along with the step http://wiki.commonjs.org/wiki/Promises/A to see how the definition, here will not go into details.
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 Method
1. Use Q. nfcall
Compared with Q. fcall, Q. nfcall is the node's Q. fcall.
The Code is as follows:
Var FS = require ('fs '),
Q = require ('q '),
Colors = require ('colors '),
File = 'example.txt ';
Var fsReadFile = Q. nfcall (FS. readFile, file, encoding );
FsReadFile. then (function (result ){
Console. log ("invoke in nfcall" + file). red );
Console. log (result. green );
}, Function (error ){
Console. log ("invoke in nfcall". red );
Console. log (error. toString (). red );
}
);
2. Use Q. denodeify
The Code is as follows:
Var fsReadFile_denodeify = Q. denodeify (FS. readFile );
FsReadFile_denodeify (file, encoding). then (function (result ){
Console. log ("invoke in denodeify". red );
Console. log (result. green)
}, Function (error ){
Console. log ("invoke in denodeify". red );
Console. log (error. toString (). red );
}
);
3. Use Q. deferd
The Code is as follows:
Var fsReadFile_deferd = function (file, encoding ){
Var deferred = Q. defer ();
FS. readFile (file, encoding, function (error, result ){
If (error ){
Deferred. reject (error. toString (). red );
}
Deferred. resolve (result );
});
Return deferred. promise;
};
FsReadFile_deferd (file). then (function (result ){
Console. log ("invoke in deferd". red );
Console. log (result. toString (). green );
}, Function (error ){
Console. log ("invoke in deferd". red );
Console. log (error. toString (). red );
}
);
4. Use makeNodeResolver ()
The Code is as follows:
Var fsReadFile_makeNodeResolver = function (file, encoding ){
Var deferred = Q. defer ();
FS. readFile (file, encoding, deferred. makeNodeResolver ());
Return deferred. promise;
};
FsReadFile_makeNodeResolver (file, encoding). then (function (result ){
Console. log ("invoke in makeNodeResolver". red );
Console. log (result. green );
}, Function (error ){
Console. log (error. toString (). red );
});
Nodejs can be used to implement a simple interface server.
Var http = require ('http'); var mysql = require ('mysql'); var connection = mysql. createConnection ({host: 'localhost', user: 'me', password: 'secret',}); // start your mysql connection. connect (); var server = http. createServer (function (req, res) {// if you send a GET request to 127.0.0.1: 1337/test? Var url_info = require ('url') If a = 1 & B = 2 '). parse (req. url, true); // check whether requestif (url_info.pathname = '/test') for/test {// use url encode for query, in this way, you can use post to send var post_data = require ('querystring '). stringify (url_info.query); // optionvar post_options of post = {host: 'localhost', port: 1337, path: '/response_logic', method: 'post', headers: {'content-type': 'application/x-www-form-urlencoded ', 'content-length': post_data.length}; // issue the postvar request_made = http. request (post_options, function (response_received) {var buf_list = new Array (); response_received.on ('data', function (data) {buf_list.push (data );}); response_received.on ('end', function () {var response_body = Buffer. concat (buf_list); res. end (response_body); connection. query ('insert ......... ', function (err, rows, fields) {// process your result}) ;}); // send the post's datare ...... remaining full text>
In nodejs, where are the descriptions of common functions found? Such as string processing, like asubstring?
Node. js uses google v8 as the javascript engine, so as long as it is a javascript object and function supported by chrome, it is available in node. js. The api on node's website is just something new to node. So you can see the javascript reference on mozilla's website. Developer.mozilla.org/en/docs/JavaScript/Reference