node.js summary

Article Directory

Preface

Simply put, Node.js is JavaScript running on the server.
Node.js is a platform based on the Chrome JavaScript runtime.
Node.js is an event-driven I/O server-side JavaScript environment. Based on Google's V8 engine, the V8 engine executes Javascript very fast and has very good performance.

1 What does a node do?

The node belongs to the backend and is used to create a high-performance web server.

    优点:1)处理高并发  2)处理IO密集比较有优势  input output
    IO密集:操作文件,网络操作,数据库...
    CPU密集:运算,压缩,解压,加密,解密....
--------------------------------------------------
客户端:
    浏览器,手机上的软件,电脑上的软件,小程序,公众号,平板的软件,智能设备...
服务器:
    本质也是一个计算机,它上面的软件和我们平常用的软件不一样,tomcat,mysql
--------------------------------------------------
web项目,主要就是接收用户请求,读取数据库中的数据,结合页面,把页面返回给客户端,所以使用node做web项目还是比较合适的。

Node creates a high-performance web server: To
put it bluntly, it means shortening the request and response time:
1) Request: The client cannot be controlled by the network depends on the network
2) Response: The response is faster
, the bandwidth of the server is increased, and cnd is accelerated... The
server is processed faster and the programmer can control
Reading and writing to the database also needs to be faster. Programmers can also control the reading and writing of the database. There is a bottleneck...
Client access to the server: Access to the server
through a URL.

java为例:
    多线程的  一个请求就会开一个线程,这个线程来处理这个请求  资源:CPU和内存

Node是单线程的:
    让一个服务员拼命的干活。

2 Use of node

Node is a JS running environment, and the browser is also a JS running environment. If JS is running in the browser, then JS is running on the front end, if JS is running in NODE, JS is running on the back end.
node环境中,只能写ES代码,不能写DOM和BOM。

node中,把文件分成三种模块:
    1)系统模块,也叫核心模块,是Node帮我们提供好的,我们直接用
        加载模块,提供了一个方法,叫require()
        载入http模块:require("http")
    2)第三方模块,是一些大神写好的模块,我们也是直接用
    3)自定义模块,我们自己写的模块
http模块:
    此模块可以创建一台服务器。
    端口:一个服务器上,可以提N个服务器,如何区分是哪一个服务器,就靠端口。 
    一共的端口数据:0~65535。0~1024不要用,知名端口也中用。暂时,你可以使用3000   5000
    监听客户端的请求:
        let http = require("http");
        let server = http.createServer();
        server.on("request",(req,res)=>{
            res.write("hello 客户端");
            res.end();
        });
        server.listen(3030,()=>{
            console.log("3030服务成功了")
        })

Run a server:
1) Right mouse button ----> run
2) Locate the code directory ----> node xxx.js If you don't want to run this server, press two ctrl (don't loosen) + c (twice)

www.baidu.com    ====>  http://www.baidu.com
baidu.com  叫域名  39.156.66.18 叫IP名  域名相比IP好记
url:
    http://127.0.0.1:3030
    http://localhost:3030/news
    http:// 协议  客户端与服务器之间通信的规则   汉语
    127.0.0.1   每一台服务器都有一个IP地址  本地电脑如果充当服务器  那么此台电脑的IP地址是127.0.0.1
    localhost   127.0.0.1对应的域名是localhost  是一个特殊的域名  每个人的电脑都一样
    3030   端口  表示你访问服务器上的哪一个服务  通常我们访问网站,不需要加端口,原因是80端口是默认的
    /news  路径  在端口后面放路径

3 npm nrm

Download third-party modules:
When we install node, it installs a downloader for us by default, called npm (node ​​package manage).
npm stands for downloader. The default is to download third-party modules from the foreign npm website,
npm -vcheck whether the installation of npm is successful, and display the version number if successful.
npm install xxxInstall the xxx third-party module by default and go to the foreign download source to find it.
install can be abbreviated as i.
nrm is a cut source tool, you can also view the source.
nrm use taobaoCut the source to Taobao source to
nrm lsview the source.
Generally, the use of node requires the following third-party modules:
npm init -yGenerate package.json file to record what third-party modules
npm i expressare used. Install and use express third-party modules (you can quickly create a server).
npm i ejsIntroduce ejs template engine: loop traversal Data display, realize the separation of data and view (ie separation of html structure and data) to
node xx.jsstart the server, but generally start in the following way.
If you modify the server code, you need to restart the server: first shut down the server, then turn on the server.
npm i nodemon -gGlobal installation of nodemon can automatically restart the server (installation is enough)
nodemon xx.js. The service needs to be started in the current directory of this js file.