当前位置:网站首页>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"); })
边栏推荐
- Software testing - interview question sharing
- Case analysis of data inconsistency caused by Pt OSC table change
- Some notes of MySQL
- AcWing 1294. Cherry Blossom explanation
- Picture coloring project - deoldify
- Codeforces Round #753 (Div. 3)
- Database advanced learning notes -- SQL statement
- DICOM: Overview
- Aborted connection 1055898 to db:
- [蓝桥杯2017初赛]方格分割
猜你喜欢
C语言读取BMP文件
Learn winpwn (2) -- GS protection from scratch
Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
MySQL与c语言连接(vs2019版)
Error connecting to MySQL database: 2059 - authentication plugin 'caching_ sha2_ The solution of 'password'
一键提取pdf中的表格
Asp access Shaoxing tourism graduation design website
AI benchmark V5 ranking
vs2019 第一个MFC应用程序
图像识别问题 — pytesseract.TesseractNotFoundError: tesseract is not installed or it‘s not in your path
随机推荐
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
Solution to the practice set of ladder race LV1 (all)
Install MySQL for Ubuntu 20.04
Are you monitored by the company for sending resumes and logging in to job search websites? Deeply convinced that the product of "behavior awareness system ba" has not been retrieved on the official w
02-项目实战之后台员工信息管理
Introduction to the easy copy module
保姆级出题教程
机器学习--人口普查数据分析
QT creator runs the Valgrind tool on external applications
项目实战-后台员工信息管理(增删改查登录与退出)
Django运行报错:Error loading MySQLdb module解决方法
Library function -- (continuous update)
Django running error: error loading mysqldb module solution
Project practice - background employee information management (add, delete, modify, check, login and exit)
人脸识别 face_recognition
解决安装Failed building wheel for pillow
Redis的基础使用
Codeforces Round #771 (Div. 2)
Tcp/ip protocol (UDP)
How to build a new project for keil5mdk (with super detailed drawings)