Node.js is a JavaScript runtime environment based on chrome’S V8 engine
Node.js is not a language, it’s not a server, it’s not a database.
advantages
1) Asynchronous non-blocking I/O (I/O thread pool) :
Asynchronous non-blocking is the ability to do one thing without affecting other people, asynchronous definitely non-blocking
I: Input O: reads and writes to output files. Database reads and writes are called I/O
2) Especially suitable for I/O intensive applications.
3) Event loop mechanism.
4) Single threading.
5) Cross-platform: Write in one place, use anywhere. Code written on Windows can also be used on Linux.
Deficiency in
- 1) Callbacks are nested too deep
- 2) Single-threaded, unable to handle CPU-intensive tasks.
- 3) Multi-core processors are not supported
Java servers are CPU intensive friendly and I/O intensive friendly.
Application scenarios of Node.js
- The web services API
- The server renders the page, improving the speed
- Back-end Web services, such as cross-domain, server-side requests
Any module (js file) in Node is wrapped by an outer file function
console.log(arguments.callee.toString())
Copy the code
function (exports, require, module, __filename, __dirname) {
console.log(arguments.callee.toString())
Exports: used to expose modules
Require: used to import modules
Module: Used to expose modules
__filename: The absolute path of the file
__dirname: the absolute path of the folder where the current file is located}
Why do we have this outer function (what does this outer function do)
- Hide internal implementation
- Support for CommonJS modularity
For the browser side, what parts of JS are grouped?
1.BOM browser object model ——- many apis (Location, history)
2.DOM document object model…….. Lots of APIS (add, delete, and check the DOM)
3.ES specification ————– ES6. ES5…..
How many parts does node-side JS consist of?
1. BOM —- is not available because the server does not need it (server does not have a navigator object)
2. No DON —- because there is no browser window
3. Almost all ES specifications are included, without alert
4. There is no window, but instead there is a global variable called global.
console.log(window)
Copy the code
Packages and package managers
Package bag
The Nodejs package basically follows the CommonuS specification. The package combines a set of related modules into a complete set of tools. A package consists of a package structure and a package description file.
- 1) Package structure: Used to organize the various files in the package.
- 2) Package description file: Describes information about the package for external reading and analysis.
What is the package
A folder on our computer, which contains certain files that fit into certain structures, is a package.
What a standard package should contain
- Package. json – description file (package specification, must have!!)
- Bin — executable binary file
- Lib — Compiled JS code
- Doc — Documentation (documentation, bug fix documentation, version changelog documentation)
- Test — some test reports
How do I turn a file into a package
Json file, and the contents of the package.json file should be legal.
Run the NPM init command
Package name requirements: Must not contain Chinese characters, must not contain capital letters, and must not be the same as other packages in the NPM repository
Relationship between NPM and node
NPM is the package management tool of the JavaScript world and is the default package management tool for the Node.js platform, as well as the largest software registry in the world
Many people now install Node just to use NPM
PNM official website: www.npmjs.com
Search the official website to find a specific package
NPM is the official package manager for node.
Back in the folder, package.json has been generated
Obtain the NPM global installation address
npm root -g
Development dependency and production dependency
- The only libraries you need to rely on for development (writing code) are the development dependencies — for example, syntax checking, compressed code, packages that extend CSS prefixes
- The packages that are essential in a production environment are production dependencies — for example, jQuery, AXIos, and so on. The so-called production environment is: the project has been developed, to be deployed to the server to run
- Some packages are both development and production dependencies — for example, jQuery
The purpose of these two dependencies is to sometimes distinguish between when the package was generated
Deleting dependency Packages
NPM Remove the dependency package name
NPM some other commands
- NPM Aduit Fix: Detects problems with project dependencies and tries to fix them
- NPM view XXXXX versions: Displays all versions of the XXXX package in the NPM repository
- NPM view XXXXX version: displays the latest version of the XXXX package in the NPM repository
- NPM LS XXXX: Check the version of the XXXX package we installed
Example: Install a certain version of jQuery
A note about the version number
“^3.x.x” : locks the maximum version. In future installation packages, ensure that the package version is 3.x.x. By default, x is the latest version
“~3.1.x” : locks the minimum version. When installing the package in the future, ensure that the package is 3.1.x. By default, x is the latest version
“3.1.1” : lock the complete version. Ensure that the package is 3.1.1 when installing the package
Node.js creates a JS file and imports the package
Use an import statement
// Import file
import Vue from 'vue'
// 1.1 Importing routing packages
import VueRouter from 'vue-router'
Copy the code
webpack
Webpack is a front-end resource loading/packaging tool. It performs a static analysis based on the dependencies of modules, and then generates the corresponding static resources for those modules according to the specified rules.
Chinese documentation: www.webpackjs.com/
Install Webpack
Before you can install Webpack, your local environment needs to support Node.js.
npm install webpack -g
Copy the code
Webpack itself can only handle JavaScript modules, and if you want to handle other types of files, you need to use loader for conversion.
So if we need to add CSS files to our application, we need to use csS-Loader and style-loader. They do two different things. Css-loader will go through the CSS file and find the URL () expression and process them. The style-loader inserts the original CSS code into a style tag on the page.
// When you enter the webpack command directly on the console, webpack does the following: //1. First, Webpack finds that there is no entry and exit in the form of a command //2. Webpack goes to the root of the project and looks for a configuration file called 'webpack.config.js' //3. When the configuration file is found, Webpack will parse and execute the configuration file. When the parsing is complete, you will get the configuration object that the configuration file exported //4. When WebPack gets the configuration object, it gets the entry and exit specified in the configuration object, and then packs and buildsCopy the code
Learn together, improve together -.-, if there are mistakes, you can comment