Windows Node JS Environment Setup and Nodejs simple understanding

Source: Internet
Author: User

One: Installation

Download Address: https://nodejs.org/en/, click to see the following image as shown in the content.


As shown in the figure above, I chose the V4.4.7 version to download, and then choose the default installation can be. Second: after the installation of the environment verified successfully, open the console, enter node, and then the data JS function to verify it. The Os.platform () shown in the following illustration is used to display the current operating system version, and the PROCESS.CWD () user displays the directory address of the current process. Enter. Exit to exit the interactive command. To this environment, the basic configuration was successful.
Three: Nodejs's simple understanding 1. After the callback function is implemented for 2 seconds, a string such as I am here is displayed in the terminal.

settimeout (function (err,data) { 
console.log (' I am here ');
},2000);
2. Basic information of operating system
Os.platform (); View operating system Platform
Os.release ();//View operating system version	
Os.type ();  View the operating system name
Os.arch ();  View operating system CPU architecture
3. Built-in object Process
PROCESS.CWD (); Displays the current directory
Process.chdir ("D:"); Toggle Directory Here is the switch to D disk
process.stdout.write (' Hello World ' + ' \ n ');/standard output here output Hello World and wrap
process.stderr.write (" Test "); Standard error output here output Test
process.stdin.read ()//Standard input stream
process.exit (code);//exit program parameter codes is returned after exit, if omitted, return 0
by default. Process.on ()//Monitor event
process.stdin.setEncoding ("UTF8");//Set code here is the input encoding set to the UTF8 output encoding setting
For more information please refer to http://nodeapi.ucdok.com/#/api/process.html 4. File Operation 1. Write to File

Fs.writefile (filename, data, code, callback); Write file code--optional parameters correspond to: filename, data, encoding, callback function (ERR)
//e.g.
var fs= require ("FS"); 
Fs.writefile (' test.txt ', ' Hello world! ', function (err) {
   if (err) throw err;
   Console.log (' Saved success! '); File is saved
});

2. Add Content

Fs.appendfile (filename, data, code, callback); Add content to a file code--optional parameters correspond to: filename, data, encoding, callback function (ERR)
//e.g.
var fs= require ("FS");
Fs.appendfile (' test.txt ', ' data to append ', function (err) {
   if (err) throw err; 
    The data is added to the tail of the file
    console.log (' The ' data to append ' is appended to file! '); 
});

3. Determine whether a file exists

Fs.exists (filename, callback); Determine if the file exists with a parameter corresponding to: file, callback function (exists)
//e.g.
var fs= require ("FS"); 
Fs.exists (' Test.txt ', function (exists) {
  Console.log (exists?) "Existence": "No existence!");

4. File renaming

Fs.rename (Oldfilename, NewFileName, callback); The file Rename parameter corresponds to: Old file, new file, callback function (ERR)
//e.g.
fs.rename (' test.txt ', ' test_new.txt ', function (err) {
   if (err) throw err;
   Console.log (' successful modification, ');
};

5. Moving files

For example,
var fs = require (' FS ');
Fs.rename (' Nowfile/test.txt ', './test.txt ', function (err) {
   if (err) throw err;
   Console.log (' remove complete ');
};

6. Read the file

Fs.readfile (filename, code, callback); Write file code--optional parameters correspond to: file, encoding, callback function (err, data)
//e.g.
var fs = require (' FS '); 
Fs.readfile ('./test.txt ', ' UTF8 ', function (err, data) {
  if (err) throw err;
  Console.log (data);


7. Delete Files

Fs.unlink (filename, callback); The deletion file parameter corresponds to: File, callback function (ERR)
//e.g.
var fs = require (' FS '); 
Fs.unlink ('./test.txt ', function (err) {
  if (err) throw err;
  Console.log (' successfully deleted ');
};


8. Create a table of contents

Fs.mkdir (Path, auth, callback); Create directory auth--optional parameters correspond to: path, permissions, callback function (ERR)
//e.g. 
var fs = require (' FS ');  
Fs.mkdir ('./file20160814 ', function (err) {
  if (err) throw err;
  Console.log (' Create Success ');


9. Delete directory

Fs.rmdir (path, callback); The delete directory parameter corresponds to: path, callback function (ERR)
//e.g. 
var fs = require (' FS ');  
Fs.rmdir ('./file20160814 ', function (err) {
  if (err) throw err;
  Console.log (' Delete Success ');
};


10. Reading directory

Fs.readdir (path, callback); The reading directory parameter corresponds to: directory, callback function (Err,files)
//e.g.
var fs = require (' FS ');   
Fs.readdir ('./', function (err,files) {
	if (err) throw err;
	Console.log (files);




For more information please refer to: http://nodeapi.ucdok.com/#/api/


Related Article

Contact Us

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.

Tags Index: