node

mac2022-06-30  185

1nodeJs什么?

        NodeJS 其实就是一个js的运行环境 属于后端的语言 服务器

Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。 nodeJs使用了一个事件驱动 以及非阻塞的I/O式模式 使其轻量又高效 node中的npm包是全球最大的包(垃圾)网站 (只要你安装了node 那么npm就会自动安装上)

 

 

以前的JS运行环境 浏览器 脚本引擎

V8引擎 浏览引擎 2部分 1、脚本引擎 渲染脚本 JS V8引擎 2、渲染引擎 渲染页面

nodeJs与普通的JS有什么区别?

node不需要浏览运行 nodeJS可以调用底层的API 例如文件的读写 服务器的创建 .... nodeJS中没有DOM和BOM的概念

 

普通的JS只能靠浏览器运行 不能够进行文件的读写

nodeJS的好处是什么? 1、高效 速度快 适合做高并发的项目

缺点:不适用于做大量计算的项目

工作当中:中间层的应用

node版本: LTS 稳定版本 CURRENT 测试版本

node的基本使用         window+r

查看node是否安装成功

node -v

node也可以做基本运算操作 1、node 回车 2、退出 ctrl+d 或者 ctrl+c 2次 3、cls清屏

npm是什么东西? npm 是一个网站 npm 是一个包管理工具 npm 是一条命令

包管理器: 对所有node的包进行安装 删除 更新 卸载 等等操作

 

 

模块化的规范

AMD 依赖前置 CMD 依赖就近 如何定义模块 如果导出模块

目前所学到的模块化方法有哪些? requireJS       seaJs           es6 Module           commonJS 前端               前端                前端                    后端 node

define           export                    module.exports export            default                        require

require                 import

 

 

模块化的好处?

1、高内聚 低耦合 2、解决命名冲突的问题 3、代码的复用 4、便于维护

node中的模块会分为哪几种?

1、核心模块 核心模块直接书写依赖的名称即可 2、自定义模块 需要通过module.exports 导出 require进行导入 导入的时候必须写路径 3、第三方模块 需要通过cnpm install 下载 然后在通过require进行引入

node创建服务器

//引入核心模块 const http = require("http"); //创建服务器 http.createServer((req,res)=>{ /* req:request 请求 res:response 响应 req.headers ajax({ type:"", url:"", data:{}, header:{ content-type:"application/x-www-form-urlencoded"; } }) req.url 请求的路经 req.method 请求的方式 get post http https 报文 res res.statusCode() 设置状态码 res.write() 响应 res.end() 最后的响应 write:write 多次 end: write+end 一次 res.setHeader() 设置响应内容的格式 第一个值是 content-type 第二个值是内容格式 text/plain 文本 text/html html文件 text/css css文件 application/x-javascript js文件 application/json json文件 res.writeHead() 设置响应的状态码 以及响应内容的格式 其实这个方法是 statusCode 与 setHeader的综合写法 参数1:状态码 参数2:对象 key:val Content-Typ:响应内容的格式 */ // res.statusCode = 200; // res.setHeader("Content-type","text/plain;charset=utf8"); console.log(req.url); console.log(req.method); res.writeHead(300,{"Content-type":"text/html;charset=utf8"}); res.write("123"); res.end("你好"); }).listen(9000); console.log("http://localhost:9000");

  

node如何运行文件 node 文件名称

1、安装node

2、看http https 报文

3、node的http模块 书写一个服务器

 

npm 常用的一些指令

npm install 安装pageage.json里面的配置文件 npm install <包名> -g 全局安装指定的依赖 npm install <包名> --save-dev 局部安装指定的依赖 npm update <包名> 更新依赖 npm uninstall <包名> 删除指定依赖 npm cache clear 清除缓存

如果通过npm install 安装的依赖都是从国外服务器进行下载的 cnpm 国内的代理

npm init 初始化 可以将你当前的文件变成一个node的包

 

如何将依赖传到npm官网

1、npm init 注意包名必须是全网唯一 2、npm login 3、npm publish

yarn

yarn 与 npm的不同之处? cnpm install axios jquery swiper --save-dev

npm下载的时候是同步下载的 npm不会有缓存 npm会将依赖的版本号进行锁定

yarn下载的时候是异步下载的 yarn会有缓存 yarn会将依赖的版本号进行锁定

1、安装yarn

cnpm install yarn -g npm init == yarn init npm install = yarn install npm install <包名> -g == yarn add <包名> -g npm install <包名> --save-dev == yarn add <包名> --dev npm update <包名> === yarn upgrade <包名> npm uninstall 包名 === yarn remove 包名 yarn global add <name> == npm install -g <name> yarn global bin == npm -g bin yarn add 包名 == npm install 包名 --save yarn add 包名 -dev == npm install 包名 --dev-save yarn update 包名 == npm undate 包名 更新包 yarn remove 包名 == npm uninstall 包名 删除包 yarn bin 全局安装目录 yarn cache ls 查看缓存 yarn clear 清除缓存 yarn install 安装所有包

 

转载于:https://www.cnblogs.com/9420i/p/10236919.html


最新回复(0)