3.1 Console/19 in node. js3.1.1 Console.log method/193.1.2 Console.error Method/203.1.3 Console.dir Method/213.1.4 Console.time Method and Console.timeend method/223.1.5 Console.trace Method/223.1.6 Console.assert Method/233.2 Global scope and global functions in node. js/233.2.1 Global scope/23 in
start position of File Reading. If this parameter is set to null, data is read from the current file pointer.
The callback passes three parameters: err, bytesRead, and buffer.
· Err exception
· BytesRead: number of bytes read
· Buffer: buffer object
Example:
The Code is as follows:
Var fs = require ('fs ');Fs.open('123.txt ', 'R', function (err, fd ){If (err ){Console. error (err );Return;} Var buf = new Buffer (8 );Fs. read (fd, buf, 0, 8, null, f
This article mainly introduces Node. this article describes how to use some open-source libraries to solve the garbled problem during crawling. For more information, see Node. garbled characters may occur when JavaScript captures a non-UTF-8 Chinese webpage. for example, if NetEase's homepage code is gb2312, garbled characters may occur during crawling.
The code is as follows:
Var request = require ('requ
Talking about node. js database Exception Handling, talking about node. js Exception Handling
This article describes how to handle node. js database exceptions:
NodeJs version: 4.4.4
Database Link error
It is the most troublesome
Recently written program node. data storage is required for js programs. there are many databases that js can use, such as mongodb and MySQL. However, I still think these databases are too large to be useful, making them more troublesome. So I found node-tiny, a non-relational database, a bit similar to mongodb.Tiny's
, index) {return new Promise (function (resolve, reject) {fs. readFile (file, 'utf-8', function (err, str) {if (! Err) {resolve (str)} else {reject (err) }}) ;}); return Promise. all (readFilsePromiseList );}). then (function (fileStrArray) {console. log ('after reading the so-called file: '+ fileStrArray );});
This Code indeed shows the elegance of nodejs development.
So where is the problem?
Currently, th
This article mainly introduces node. socket. io broadcast messages. For more information, see socket. the io () server has a sockets attribute. The attribute value is all socket objects connected to the client. you can use the send method or emit method of this object to broadcast messages to all clients.
Io. sockets. send ("user commected );
Io. socket. emit ("login", names );
Case
Server. js code:
The
This article mainly introduces node. url in js. description of the parse method. This article describes the url. parse method description, syntax, receive parameters, use instances, and implement Source Code. For more information, see
Method description:
Converts a URL string to an object and returns it.
Syntax:
The Code is as follows:
Url. parse (urlStr, [parseQueryString], [slashesDenoteHost])
Receivi
This article mainly introduces node. url in js. the format method is described in this document. format method description, syntax, receive parameters, use instances, and implement Source Code. For more information, see
Method description:
Convert a parsed URL object into a formatted URL string.
Syntax:
The Code is as follows:
Url. format (urlObj)
Receiving parameters:
UrlObj indicates a URL object and
This article mainly introduces how to use socket in node. for more information about how to create a namespace for I/O, see. If a developer wants to completely control the sending of messages and events in a specific application, you only need to use a default "/" namespace. however, if developers need to provide applications to other applications as third-party services, they need to define an independent namespace for a socket port used to connect t
Example of Node. js readline Reading and Writing File Content line by line, node. jsreadline
This article describes the two implementation methods of reading data row by row using readline. The details are as follows:
What is Readline?
Readline is an encapsulated module for implementing standard input and output in Node
provide the host name, IP address, Unix socket file, or the pipe bound to the server name in Windows.
2. Start the serverRun the following command:Copy codeThe Code is as follows:$ Node server. jsAccess http: // 127.0.0.1: 3000/. The browser displays the following content:Copy codeThe Code is as follows:{ Quot; statusCode quot;: 404, quot; error quot;: quot; Not Found quot "}
Normally, because there is no content on the server itself, the routin
instance is obtained, you can use net. Socket to do whatever you want. I listened to the data event to receive the data played by the server, and listened to the close event to exit the process. For specific API of net. Socket, refer to https://nodejs.org/api/net.html#net_class_net_socket.Running example
There is a saying in the comments that the time is too late. Well, you can run it. Run "node echoServer. js
', {env: envCopy}, function (err, stdout, stderr ){If (err) {throw err ;}Console. log ('stdout: ', stdout );Console. log ('stderr: ', stderr );}
In the preceding example, an envCopy variable is created to save the environment variable. It starts from process. env copies the environment variables of the
better observe program running and view errors when exceptions occur, you can use the console function in the variable console.
The Code is as follows:
Console. log ('this is a piece of log information ');Timing and output timing information on the
;geth.logYou can open a new command line terminal and enter the following command to view the log:$ tail -f geth.logREDIRECT another endpointYou can also redirect the log to another terminal, first in the terminal you want to see the log input:$ ttyYou can get the terminal number, for example:/dev/ttys003Then another terminal uses:$ geth
Declaration
In C/C ++, we declare the variable as follows:
"C ++
The Code is as follows:
Void foo (){}Int a = 0;Char B = 'a ';Float c = 1.0f;Void (* d) () = foo; //
In Node. js, it is like this:"Javascript
The Code is as follows:
Function foo (){}Var a = 0;Var B = 'a ';Var c = 1.0;Var d = foo;
Therefore, no matter what type of variable, it is solved by a var in
be included. For example:/index.html? Page = 12
Headers: request header object.
Auth: Basic Authentication (Basic Authentication). This value is calculated as the Authorization part in the request header.
Callback: callback. A parameter is passed to the http. ClientResponse instance. Http. request returns an http. ClientRequest instance.
Example:
The Code is as follows:
Var options = {Hostname: 'www .google.com ',Port: 80,Path: '/upload ',Method: 'post'}; Var req = http. request (options, fun
Node. js ~ Deployment on linux and node. jslinux
The following uses centOS as an example to deploy the node. js environment.
Start centos and download the node. js package.
curl --silen
As we all know, Nodejs's single-process non-blocking model is suitable for task-intensive (IO) rather than computing-intensive. How many of them are not suitable for computing? The following is a simple hardware platform for testing: Apple Macmini6 and Windows 8. the single-process non-blocking model of js is suitable for task-intensive (I/O) instead of computing-intensive. so how many are not suitable for computing? The following is a simple test. th
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.