NodeJS basic knowledge 1

Introduction

  • Node.js is a cross-platform JavaScript runtime environment that can run JavaScript code on the server side.
  • Most of Node's modules are written in JavaScript. Before Node appeared, JS was usually used as a client-side programming language, and programs written in JS were often run on the user's browser.
  • Node.js can be understood as a server that has implemented functions. It uses the command line to import the programmer's js code that has achieved the function into the server to run and get the result.
  • Programmers can also use CoffeeScript, TypeScript, Dart language, and other programming languages ​​that can be compiled into JavaScript language to write functional code, and then hand it over to Node.js to execute.
  • Node is single-threaded when processing requests (when programmers use the node command to execute code), but it has an I/O thread pool in the background.
  • Node is an implementation of the CommonJS specification.

CommonJS specification

  • The CommonJS specification was proposed mainly to make up for the current lack of modular standards for Js.
  • CommonJS's definition of a module is very simple, with only three aspects:
    module reference,
    module definition,
    module identification
  • The realization of the CommonJS specification by Nodejs is to achieve the above three aspects.

Modular

  • In node, every js file written by a programmer is a module.
  • In Node, the js code in each js file runs independently in a function instead of the global scope, so the variables and functions in one module cannot be accessed in other modules.
var a = 10;//js中如果将var去掉,a是全局变量

/*
	在node中有一个全局对象 global,它的作用和网页中window类似
	js中全局变量都是window的属性,局部变量不是。window可以引该变量
		在全局中创建的变量都会作为global的属性保存
		在全局中创建的函数都会作为global的方法保存

	当node在执行模块中的代码时,它会首先在代码的最顶部,添加如下代码
 			function (exports, require, module, __filename, __dirname) {

 	在代码的最底部,添加如下代码
 			}

 	实际上模块中的代码都是包装在一个函数中执行的,并且在函数执行时,同时传递进了5个实参
		 exports
		 	- 该对象用来将变量或函数暴露到外部,只有使用该参数修饰的变量或方法才能被其他模块所引用
		 	- exports 和 module.exports
		          - 通过exports只能使用.的方式来向外暴露内部变量或方法
			         exports.xxx = xxx

		          - 而module.exports既可以通过.的形式,也可以直接赋值
			          module.exports.xxx = xxxx
			          module.exports = {}
			          
		 require
		 	- 函数,用来引入外部的模块

		 module
		 	- module代表的是当前模块本身
		 	- exports就是module的属性
		 	- 既可以使用 exports 导出,也可以使用module.exports导出

		 __filename
 			C:\Users\lilichao\WebstormProjects\class0705\01.node\04.module.js
 			- 当前模块的完整路径

	  	 __dirname
 			C:\Users\lilichao\WebstormProjects\class0705\01.node
 			- 当前模块所在文件夹的完整路径

* */
//console.log(global.a);

/*
	arguments.callee
		- 这个属性保存的是当前执行的函数对象
* */
//console.log(arguments.callee + "");
//console.log(arguments.length);

//console.log(exports);
//console.log(module.exports == exports);

console.log(__dirname);

Introduce other modules

  • In node, external modules are introduced through the require() function. This method can pass a file path as a parameter, and node will automatically introduce external modules according to the path. Note: If you use a relative path, it must start with "." or "..".
  • After the module is imported using require(), the function will return an object, which represents the imported module, through which the methods or variables modified by exports in the imported module can be obtained.
/*我们使用require()引入外部模块时,使用的就是模块标识,我们可以通过模块标识来找到指定的模块
	- 模块分成两大类
		核心模块
			- 由node引擎提供的模块
			- 核心模块的标识就是,模块的名字
		文件模块
			- 由用户自己创建的模块
			- 文件模块的标识就是文件的路径(绝对路径,相对路径)
				相对路径使用.或..开头

 */

var math = require("./math");//用户自定义的模块
var fs = require("fs");//核心模块

console.log(math.add(123,456));
console.log(fs);
  • Operation mode: Enter the command "node index.js" in the command line window, "index.js" is the file name of the js file.

Guess you like

Origin blog.csdn.net/qq_36828822/article/details/105880200