Nodejs1. Install Nodejs Download the latest version of node from the Nodejs website and set the environment variable so that you can operate the NPM environment variable directly from the command line under cmd: path d:/ Nodejs View native node and NPM version 2. Copy a small script directly from the official website: nodeexample.js
Const HTTP = require (' http ' = ' 127.0.0.1 ' ' Hello world\n ' => {console.log (' Server running at http: // ${hostname}:${port}/'); });
Can be run directly through the console: Open the browser input http://127.0.0.1:3000/, the page appears Hello world if the Hello world is changed to Hello NodeJs, refresh the browser to find the page is still unchanged, At this time to run the JS file again in the console, CTRL + C end the last activity 3. NPM for node Npmnodejs is like Maven in Java, which is a package management tool that is installed with Nodejs. No, it's the equivalent of everything. The official mirror site of NPM is Https://www.npmjs.com/node Package Manager is a command-line utility that allows you to find, install, delete, publish, and do many things related to the Node encapsulation module, Here are some common command-line options: version This command can view a series of versions such as node NPM v8  NPM Versionsearch & nbsp Find module packages in repositories  NPM search Expressinstall use a PA in a repository or local location Ckage.json file to install package  NPM install expressinstall-g Set up a package in a globally accessible location NPM install express-gremove Delete a module &NB SP;NPM Remove Expresspack The modules defined in a Package.json file are encapsulated as. tgz files NPM PAC Kview Show details of the module  NPM View Expresspublish publish modules defined in a Package.json file to the registry NPM Publishunpublis H Unpublish A module you have published NPM unpublish mymoduleowner allow you to add, remove, and list the owner of a package in the repository  NPM Add ownername mymodule/npm rm ownername MYMODULE/NPM LS mymodule Note that the NPM Install command does not refer to This is because NPM will find a Package.json file by default when you need additional modules, add those modules to the dependent instructions, and run NPM install again. The dependent instruction is in the dependencies of the Package.json file. 4. About Package.json When we download the appropriate plugin via NPM install, the Node_modules folder appears automatically in the project, such as NPM Install Gulp, and we can see Node_ Directory structure of the Gulp folder under the modules file:
Note the following package.json, which is a configuration file that Nodejs and NPM will automatically read, which is a standard JSON format string.
If we in the external JS file directly require (' Slib '), Nodejs will automatically: 1) See if it has a built-in module, if there is a priority to load the built-in Module 2) if not see whether it is a "path-level" Reference 3) is not in the Node_ Modules look for folders with the same name. First, the default is to look for index.js, if not it will see whether the main definition built-in modules in Package.json such as require (' HTTP '), the path level such as require ('./xxx.js '), note here. Represents the current JS file is located in the directory,. JS can write not write, when downloading gulp because there is a reference between the package parking, so some other plugins will be downloaded. We can also customize plugins in Node_modules, such as creating a new folder in Node_modules, The JS file inside must be defined as index.js, so when we refer to this JS file, node will automatically load the file under the Index.js, for example:
5. Custom Plugins
If we want to customize a plug-in under Node_modules, referenced by an external file, and not automatically loaded by index.js, how do we define it?
1) Create a new file under Node_modules, Examplejs, including Aaa.js and Package.json
Packeage.json Only one line:
{ "main": "Aaa.js"}
Here the main refers to the entrance, note: If the aaa.js here is named Index.js, then we do not need to Package.json, node will automatically load each plug-in index.js file 2) Create a new Showname.js file in the project, referencing the Examplejs above
var getlib=require (' Examplejs '); Getlib.showname ();
3) Execution procedure:
So how do you add it to the HTML? 1) New index.html, the introduction of JS to
<!DOCTYPE HTML><HTMLLang= "en"><Head> <MetaCharSet= "UTF-8"> <title></title></Head><Body><Scriptsrc= "Showname.js"></Script></Body></HTML>
2) Start the browser, error:
The browser does not recognize the require, that is, the current Web page does not support require the way to get getlib by require (' Examplejs '), print getlib, add a line in Showname.js:
Console.log (Getlib);
View results:
The found Getlib is an object modification showname.js
var getlib=require (' Examplejs '); for (var in getlib) { Console.log (a+ ":" +getlib[a]);}
Execution Result:
Can be found that Showname.js has been getlib in the variables and functions are drawn out, this time index.html can refer to the JS
NodeJs Installing NPM nodemodules Package.json