Recently began to learn about node. js, here to see the recent (Seven days learn Nodejs) about a string, deepen understanding.
node. JS is a parser-a running environment that allows JS to do something with the built-in objects and methods provided by the running environment. For example, Fs/http built-in objects. --JavaScript running on the server
1 start with a small example of the actual point, feel the Nodejs
var http = require (' http '); Http.createserver (function (request, response) { Response.writehead ($, {' Content-type ': ' Text/plain '}); Response.End (' Hello world\n ');}). Listen (8888); Console.log (' Server running at http://127.0.0.1:8888/');
This creates an HTTP server. On the http://127.0.0.1:8888, you can see the output of Hello World with a browser access.
2 Modules
Require// reference module
Referencing other modules in Main.js
var hello=require ('./hello.js ');
Exports// export object to the module used for require module
exports.hello=function() {console.log (' Hello World ');}
function hello () {console.log (' Hello World ');} Exports.hello=hello;
Module//Can access information for the current module, primarily for replacing the default export object of the module
Note: The code in the module is only executed once at initialization time, and calls cache reuse when reused
When there are multiple modules in a package, the main module can be named Index.js, or Package.json, through the directory as the access to the package (not the file path);
package.json{ "name": the folder where the current folder of the "package"/ / module is located // / Home/node/package/lib/xx.js}// Access Module require ('/home/node/package ') ha, A look is the feeling of visiting the package
4 NPM
Get third-party packages
Download Third Party package NPM install AGRV;
or configure it in the Package.json.
{ "name": "Node-echo", "main": "./lib/echo.js", "dependencies": { "argv": "0.0.2" }}
5 Formal little copy of the small file
var // built-in file templates function Copy (SRC,DST) { fs.writefilesync (Dst,readfilesync (SRC)}function Main (argv) { copy (argv[0],argv[1]);} Main (process.argv.slice[2]);//argv[0],argv[2] The absolute path of the executing program and the master module stored separately
These small files belong to one-time write memory, read out at once, will cause memory explosion
Write large files (flow-through)
var fs = require (' FS '); function copy (src, DST) { fs.createreadstream (SRC). Pipe (Fs.createwritestream (DST)); (read side write)} function Main (argv) { copy (argv[0], argv[1]);} Main (Process.argv.slice (2));
6 Event object, Eventemitter time emission and event monitoring function encapsulation
Each event has an event name (string) and several parameters, and an event can have multiple event supervisors
var events = require (' Events 'varnew events. Eventemitter (); Emitter.on (function(arg1, arg2) {//Register event Listener console.log (' first event ', Arg1, arg2);}); Emitter.on (function(arg1, arg2) { console.log (' second event ', Arg1, arg2);}); Emitter.emit (' someevent ', ' Byvoid ', 1991); Launch event, call event listener in turn
Eventemitter API:
Eventemitter.on (Event,listener);
Eventemitter.once (Event,listener); event is only released once
Eventemitter.removelistener (event,listener); removing events
Error is a special event for Eventemitter
Encountered exception is the launch Error event, call the listener function, to avoid the program crashes
var events = require (' Events 'var emitter = newevents. Eventemitter (); Emitter.on (' error ',function() { console.log (' Error ')}) Emitter.emit (
About inheritance: Most of the time we don't use eventemitter directly, but inherit it from the object. Including FS, NET, HTTP, as long as the core modules that support event response are Eventemitter subclasses.
7 Global Variables
Process
PROCESS.ARGV command line parameter array
Process.stdout/process.stdin
Process.stdin.resume (); Process.stdin.on (function(data) {Process.stdout.write (' Read From console: ' +
Process.nexttick (callback), set the task for the time cycle, the next loop response thing call callback, the complex work split into small time, improve response speed, save response time (I understand this is not very good)
(Write these today, then go on to write them later)
A shallow understanding of node. js