This article uses several examples to analyze how to use the require () function in node. js to load modules. It is very detailed. For more information, please refer to the detailed descriptions below and write them in comments. Here we will not be so embarrassed. Let's take a look at the details here. Please never comment it out as air.
The Code is as follows:
/* In
looks like this:
The Code is as follows:
Var myModule2 = require ('./mymodule2 ');
MyModule2.printA (); //->
MyModule2.printB (); //-> B
Console. log (myModule2.pi); //-> 3.141592653589793
Load Module
As mentioned above, you can use the require function to load modules. You don't have to worry that calling require in code will affect the global namespace, because Nod
looks like this:
The Code is as follows:
Var myModule2 = require ('./mymodule2 ');
MyModule2.printA (); //->
MyModule2.printB (); //-> B
Console. log (myModule2.pi); //-> 3.141592653589793
Load Module
As mentioned above, you can use the require function to load modules. You don't have to worry that calling require in code will affect the global namespace, because Nod
This article mainly introduces node. use q. js implements api promise. promise is a standard that describes the returned results of asynchronous calls, including correct returned results and error handling, need a friend can refer to what is promise and promise solution is what problem, please experience node callback asynchronous coding method, along with the st
completed the setup, but you don't know if it can run correctly, let's write an example file to test it.
Create example. js in the C: cygwin directory
Input
Var http = require ('http ');
Http. createServer (function (request, response ){
Response. writeHead (200, {'content-type': 'text/html '});
Response. end ('Hello World');
}). Listen (8888 );
Console. log ('s
Console.warn ("Nothing selected, can ' t validate, returning nothing"); Run alone, incredibly printed out in the console nothing selected, can ' t validate, returning nothing, Enlightened, since this can be printed in the console of the message, then save the trouble to use what alert or press F10 to go through the debugging, it is certain that it can be saved a lot of time. Specifically, using the for (var
:
The Code is as follows:
Var buffer1 = new Buffer (8 );Buffer1.write ('nice to meet U', 'utf8 ');Var buffer2 = new Buffer (8 );Buffer1.copy (buffer2 );Console. log (buffer2.toString (); // nice to meet u
Stream module
In UNIX operating systems, stream is a standard concept. There are three major streams:
1. Standard Input2. standard output3. Standard Error
Readable stream
If the buffer zone is the method
In addition to Console. log (), more Javascript Debugging commands are provided,
The Console object provides access to the browser Console (for example, the Firefox Web Console ). Different browsers work in different ways, but here we will introduce some interface features p
functions as with regular objects. For example, assign a function to a variable, pass these parameters to the method, declare them as object attributes, or even return them from the function.
Callback is an asynchronous function in JS and can be passed as a parameter to another function or executed or returned from another function before execution. This is the basic concept of callback.
When we pass a callback function as a parameter to another func
defined previously and is mapped to/routes/user. js. We will call the list function defined in this file. The user list is displayed here.
Continue to add:
The Code is as follows:
Http. createServer (app). listen (app. get ('Port'), function (){Console. log ('express server listening on port' + app. get ('Port '));});
Finally, create an http server and start i
defined previously and is mapped to/routes/user. js. we will call the list function defined in this file. The user list is displayed here.
Continue to add:
The code is as follows:
Http. createServer (app). listen (app. get ('port'), function (){Console. log ('express server listening on port' + app. get ('port '));});
Finally, create an http server and start i
path.
Node. js Website: http://nodejs.org/
Enter the tar xf node-v0.4.7.tar.gz in the Command window, node-v0.4.7.tar.gz for your own version.
Enter the cd node-v0.4.7 in the Command window and press Enter.
Enter./configure in the Command window and press Enter.
Enter make
an example of loading files to memory under a Node:
The Code is as follows:
Var fs = require ('fs ');
Fs. readFile ('/etc/passwd', function (err, fileContent ){
If (err ){
Throw err;
}
Console. log ('file content', fileContent. toString ());
});
In this example, you pass an inline anonymous function as fs. the second parameter of readFile is actually using CPS
interactively and view the running results.Compile a test programWe can also write a very simple terminal program to test whether the installation is successful and works properly. To do this, we will create a "test. js" file, which contains the following code:
root@ubuntu-15:~# vim test.jsvar util = require("util");console.log("Hello! This is a Node Test Program");:wq!
Now, in order to run the above pro
GB, the entire thread is suspended and the execution continues after the file is read. In other words, I/O operations block code execution, greatly reducing program efficiency.
Asynchronous I/O is actually no stranger to front-end engineers, because initiating Ajax requests is the most common "Asynchronous" call. In Node, taking reading a file (reading a file is a time-consuming I/O operation) as an example, it is similar to the method used to initia
This article provides a summary of how node. js implements file operations, which is very detailed and comprehensive. I hope you will enjoy Node. js and file operations in the same way as other languages. Not to mention node. in js
(read the file and then perform subsequent operations)
Var fs = require ("fs"); var data = fs. readFileSync ('/fs.txt'); console. log (data. toString (); console. log ("program execution is finished! ");
Output result:
"File content"
"Program execution is finished !"
2. Non-blocking call (synchronous execution of re
This article mainly introduces how to easily create a node. js server (5): event processing program. This series of articles will create a complete node. js server step by step, if you need it, you can refer to the introduction of an event processor module to provide different feedback for different requests.
This mod
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.