基础常识
网页渲染流程:其中会先经历HTML解析器,接着遇到css会使用css解析器,遇到js会使用JS引擎。
Chrome V8引擎:JavaScript引擎实现,主要有两个阶段:编译与执行,在用户使用时完成编译并执行。
渲染引擎:能够将HTML/CSS/JavaScript文本及相应的资源文件转换成图像结果。主要作用就是将资源文件转化为用户可见的结果。
可参考文章:Chrome V8引擎介绍
JavaScript语言:是一种解释性语言,边解释边执行(在执行时已完成编译,可直接执行)。运行时确认变量类型,正因为该特性,在运行时计算和决定类型,则会严重影响语言的性能,这也是js运行效率比c++、java低很多的原因。
一、认识Node.js
下载安装
官网:Node.js。下载地址:Node.js—Downloads
1.1、Node.js的诞生
曾经的js只能运行在浏览器中,node.js出现之后不管是服务器上,还是在自己笔记本中只要安装了node.js,就可以运行js代码了。
Node.js:基于Chrome V8引擎的js运行环境(或者说是一个js语言的解释器),基于C++开发。V8引擎执行Javascript的速度非常快,性能非常好。web前端的js代码最终还是运行在浏览器中的。运行时或产品环境下不依赖于node.js。
诞生初衷:是为了提升服务器的效率(初始是用来写服务器端的),没想到在前端中大放异彩。原本后端需要使用java、ruby、c++来写,出现node.js之后可以使用js来写后端了。现在成为前端开发的一个基本设施。
node.js的出现让前端大爆发。如react、vue.js这些的前端框架开发的环境变得强大,node.js就是这些开发环境运行的基础。
两大优点:事件驱动,非阻塞IO。
当时成就node.js的满足条件:①js符合非阻塞模型以及事件驱动。②Chrome的V8引擎开源。
目的说明
对于前端,不懂PHP,Python等动态语言,想创建服务!
对于后端,想要部署一些高性能的服务!
1.2、node.js运行代码的两种方式
方式一:进入node.js的交互环境命令:node
1、node:能够进入node.js的交互环境。例如普通的加法能够进行运行执行。
退出交互环境:ctrl+d。
说明:一般常见是将代码写入到文件中输出。
方式二:创建文件命令以及执行js文件的命令,将代码写入文件,接着使用命令对js文件解释执行。
创建文件命令:code xx.js。
执行js文件:node xx.js
1.3、node.js与浏览器的部分区别
介绍三个不同点:
①全局变量使用不同。
②在node.js环境中无法使用document对象了,浏览器可以使用。在node.js环境中有http对象。
C:\Users\93997>node -- 进入node.js的交互环境 Welcome to Node.js v12.18.3. Type ".help" for more information. > http -- 查看http对象 { _connectionListener: [Function: connectionListener], METHODS: [ 'ACL', 'BIND', 'CHECKOUT', 'CONNECT', 'COPY', 'DELETE', 'GET', 'HEAD', 'LINK', 'LOCK', 'M-SEARCH', 'MERGE', 'MKACTIVITY', 'MKCALENDAR', 'MKCOL', 'MOVE', 'NOTIFY', 'OPTIONS', 'PATCH', 'POST', 'PROPFIND', 'PROPPATCH', 'PURGE', 'PUT', 'REBIND', 'REPORT', 'SEARCH', 'SOURCE', 'SUBSCRIBE', 'TRACE', 'UNBIND', 'UNLINK', 'UNLOCK', 'UNSUBSCRIBE' ], STATUS_CODES: { '100': 'Continue', '101': 'Switching Protocols', '102': 'Processing', '103': 'Early Hints', '200': 'OK', '201': 'Created', '202': 'Accepted', '203': 'Non-Authoritative Information', '204': 'No Content', '205': 'Reset Content', '206': 'Partial Content', '207': 'Multi-Status', '208': 'Already Reported', '226': 'IM Used', '300': 'Multiple Choices', '301': 'Moved Permanently', '302': 'Found', '303': 'See Other', '304': 'Not Modified', '305': 'Use Proxy', '307': 'Temporary Redirect', '308': 'Permanent Redirect', '400': 'Bad Request', '401': 'Unauthorized', '402': 'Payment Required', '403': 'Forbidden', '404': 'Not Found', '405': 'Method Not Allowed', '406': 'Not Acceptable', '407': 'Proxy Authentication Required', '408': 'Request Timeout', '409': 'Conflict', '410': 'Gone', '411': 'Length Required', '412': 'Precondition Failed', '413': 'Payload Too Large', '414': 'URI Too Long', '415': 'Unsupported Media Type', '416': 'Range Not Satisfiable', '417': 'Expectation Failed', '418': "I'm a Teapot", '421': 'Misdirected Request', '422': 'Unprocessable Entity', '423': 'Locked', '424': 'Failed Dependency', '425': 'Unordered Collection', '426': 'Upgrade Required', '428': 'Precondition Required', '429': 'Too Many Requests', '431': 'Request Header Fields Too Large', '451': 'Unavailable For Legal Reasons', '500': 'Internal Server Error', '501': 'Not Implemented', '502': 'Bad Gateway', '503': 'Service Unavailable', '504': 'Gateway Timeout', '505': 'HTTP Version Not Supported', '506': 'Variant Also Negotiates', '507': 'Insufficient Storage', '508': 'Loop Detected', '509': 'Bandwidth Limit Exceeded', '510': 'Not Extended', '511': 'Network Authentication Required' }, Agent: [Function: Agent] { defaultMaxSockets: Infinity }, ClientRequest: [Function: ClientRequest], IncomingMessage: [Function: IncomingMessage], OutgoingMessage: [Function: OutgoingMessage], Server: [Function: Server], ServerResponse: [Function: ServerResponse], createServer: [Function: createServer], get: [Function: get], request: [Function: request], maxHeaderSize: [Getter], globalAgent: [Getter/Setter] }
③对于ES6新特性的支持程度不同。


