当前位置:网站首页>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"); })
边栏推荐
- [download app for free]ineukernel OCR image data recognition and acquisition principle and product application
- 天梯赛练习集题解LV1(all)
- Asp access Shaoxing tourism graduation design website
- Solution to the practice set of ladder race LV1 (all)
- Neo4j installation tutorial
- Software testing - interview question sharing
- vs2019 使用向导生成一个MFC应用程序
- 学习问题1:127.0.0.1拒绝了我们的访问
- Reading BMP file with C language
- Some notes of MySQL
猜你喜欢
Vs2019 desktop app quick start
QT creator specifies dependencies
MySQL master-slave replication, read-write separation
Basic use of redis
QT creator create button
02 staff information management after the actual project
Leetcode 461 Hamming distance
人脸识别 face_recognition
AcWing 1298.曹冲养猪 题解
Django running error: error loading mysqldb module solution
随机推荐
When you open the browser, you will also open mango TV, Tiktok and other websites outside the home page
安全测试涉及的测试对象
Why can't I use the @test annotation after introducing JUnit
解决安装Failed building wheel for pillow
保姆级出题教程
QT creator create button
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)
TCP/IP协议(UDP)
Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
What does BSP mean
Install mongdb tutorial and redis tutorial under Windows
Reading BMP file with C language
QT creator test
ES6 promise object
[AGC009D]Uninity
Did you forget to register or load this tag
Ansible实战系列一 _ 入门
图片上色项目 —— Deoldify
软件测试-面试题分享
vs2019 桌面程序快速入门