当前位置:网站首页>nodejs 详解
nodejs 详解
2022-07-06 09:14:00 【进阶日记】
node.js
安装nodejs
https://nodejs.org/zh-cn/
包管理器npm
npm init 初始化 npm install 下载依赖 && npm install 下载依赖@版本号 //安装包 -S -save 的缩写 用于生产(上线)环境下,包名会被注册在package.json的dependencies里面,在生产环境下这个包的依赖依然存在; -D -dev(生产) 包名会被注册在package.json的devDependencies里面,仅在开发环境下存在的包用-D,如babel,sass-loader这些解析器 devDependencies ,是我们开发的时候需要用到的一些包,只需要用于开发阶段,真正打包上线的时候并不需要这些包,因为这些工具只是你用来打包代码的,是用来识别特定文件以及代码,帮助我们生产最终文件的 dependencies,这个则是需要发布到生产环境中的,就比如你要跑一个基于vue的项目,所以需要vue.js来支持,而这个vue.js文件就需要跟随项目到最终的生产环境 -g 是全局安装 npm uninstall 包名 //卸载包 npm run 运行项目
nrm
nrm ls //查看可选的下载源 nrm use name //切换下载源 nrm add registry 下载源 //添加下载源 nrm del name //删除下载源 nrm test//测试下载响应时间
nvm管理node版本
nvm list available //列出所有可以安装的node版本 nvm install 17.2.0 //node版本号下载 nvm ls //显示已下载的node版本 前带*的为当前使用的node版本 nvm use 17.2.0 //使用node版本
npx
npx @vue/cli -V相当于npm install @vue/cli -g + vue -V 1、临时安装可执行依赖包,不用全局安装,不用担心长期的污染。 2、可以执行依赖包中的命令,安装完成自动运行。 npx create-react-app my-react-app //下载到临时目录使用后自动删除 3、自动加载node_modules中依赖包,不用指定$PATH。 node-modules/.bin/mocha --version npx mocha --version 4、可以指定node版本、命令的版本,解决了不同项目使用不同版本的命令的问题。 npx [email protected] -v
模块化
导出模块的三中方式 module.exports = { function add(a,b){} return a+b } console.log(add(1,2)) } module.exports.name = 'zs' exports.num = 18 使用require导入模块 const index = require('./index');
fs模块
fs.readFile(path[,options],callback) fs.writeFile(path,'内容',callback) // 导入fs const fs = require('fs') // 调用fs.writeFile()方法写入文件 // 参数1:写入文件的存放路径 // 参数2:写入文件的内容,格式一般为utf8 // 参数3:回调函数 fs.writeFile('./node.txt','asdfsaf',function (err) { if (err) { return console.log('文件写入失败',err.message); } console.log('文件写入成功'); }) // 调用fs.readFile()方法读取文件 // 参数1:读取文件的存放路径 // 参数2:读取文件时候采用的编码格式,一般为utf8 // 参数3:回调函数,拿到失败和成功的结果 fs.readFile('./node.txt','utf8',function (err,succeed) { if (err) { return console.log('文件读取失败',err); } console.log('文件内容为:',succeed); })
dirname
//__dirname表示当前文件所处的目录 console.log(__dirname); //输出E:\nodeStudy 可以用__dirname+'./node.txt'拼接文件路径
path模块
path.join() //拼接文件路径 path.basename() //获取文件路径中的文件名 path.extname() //获取路径中文件拓展名 //导入path模块 const path = require("path") const pathStr = path.join('a','b','c','..','d/f') console.log(pathStr);//a\b\d\f path.join(__dirname+'/node.txt')//拼接文件路径 path.fpath = 'a/b/c/index.html' console.log(path.basename(fpath))//获取文件名index.html console.log(path.basename(fpath,'.html'))//获取文件名前缀index console.log(path.extname(fpath))//.html
http模块
// 导入http模块 const http = require("http") // 创建web服务器实例 const serve = http.createServer() // 为服务器实例绑定request事件,监听客户端的请求 // req为请求对象,包含了与客户端相关的数据和属性 //res响应对象,包含了与服务器相关的数据和属性 serve.on('request',(req,res)=>{ console.log(req.url,req.method); console.log('request事件'); res.setHeader("Content-Type","text/html;charset=utf-8");//若不设置编码格式使用中文返回会乱码 res.end("请求成功返回数据") }) // 启动服务器 serve.listen(8088,()=>{ console.log("服务器启动成功:http://127.0.0.1:8088"); })
边栏推荐
- Vs2019 use wizard to generate an MFC Application
- Antlr4 uses keywords as identifiers
- Attention apply personal understanding to images
- Learn winpwn (3) -- sEH from scratch
- 天梯赛练习集题解LV1(all)
- Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
- Machine learning notes week02 convolutional neural network
- Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
- Punctual atom stm32f103zet6 download serial port pin
- QT creator specify editor settings
猜你喜欢
引入了junit为什么还是用不了@Test注解
LeetCode #461 汉明距离
QT creator runs the Valgrind tool on external applications
机器学习--人口普查数据分析
C语言读取BMP文件
Learning question 1:127.0.0.1 refused our visit
Learn winpwn (3) -- sEH from scratch
Install mysql5.5 and mysql8.0 under windows at the same time
Vs2019 first MFC Application
Picture coloring project - deoldify
随机推荐
QT creator support platform
Django running error: error loading mysqldb module solution
AcWing 179. Factorial decomposition problem solution
Codeforces Round #771 (Div. 2)
What does BSP mean
Some notes of MySQL
解决安装Failed building wheel for pillow
QT creator uses Valgrind code analysis tool
L2-007 家庭房产 (25 分)
Rhcsa certification exam exercise (configured on the first host)
ES6 let and const commands
Julia 1.6 1.7 common problem solving
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
Request object and response object analysis
L2-001 紧急救援 (25 分)
Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
vs2019 桌面程序快速入门
Install MySQL for Ubuntu 20.04
One click extraction of tables in PDF
In the era of DFI dividends, can TGP become a new benchmark for future DFI?