打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
node.js基本操作
node.js:
基于Chrome V8引擎的JavaScript运行环境,让其运行在服务端的开发平台。自身提供了基本模块,也可通npm自行下载模块
特点:快、耗内存多。没有浏览器安全级别限制进行解析js代码、提供很多系统级别的api
框架的选择:
比较热门的包括:
Express(Sails),koa,Hapi
Express:完善、稳定、文档全、社区大
koa :超前(koa2)
Hapi:复杂(一个简单的helloworld都要做很多堆砌),适合复杂的大型项目
egg(阿里巴巴团队刚推出的)
Node.js的安装版本:
Node.js版本: LTS(长时间支持版本——-推荐)
Current(最新版本,有些api会有变动)
在node环境下执行js文件:终端进入文件所在目录,在终端中输入 node 文件名
node.js模块开发规范
自定义模块
var obj = {这里可以写函数,写对象}
module.exports = obj
加载自定义模块
var obj = require('自定义的文件的名称')
例:文件名称为01.js
let a = { username:'aaa', log:function(){ console.log('hello world') }}// 暴露 模块 a 对象module.exports = a;
在文件名称02.js中引用
// 获取到了 01.js 暴露的模块let box = require('./01')// 目录 模块let bxios = require('./bxios')console.log(box.username)box.log()bxios()console.log(222)console.log(333)
在整个目录暴露时,只读取文件名为index.js这一个文件
安装好之后进入方法  window+r 或者在想打开的文件的地址栏输入cmd。或者在vscode的文件里选择在终端中打开。或者在开始菜单搜索cmd(该方式可以用管理员方式运行)
这样就可以使用npm的命令
1.npm -v :查看npm安装的版本
2.npm init:会引导你创建一个package.json文件,包括名称、版本、作者等信息。
使用npm init-y 可以跳过很多回车步骤
3.npm list:查看当前目录下已安装的node包 (简写:npm ls)
4.npm install moduleNames:安装nde模块到本地目录node_modules下
5.npm install <name> -g  将包安装到全局环境(install可以简写为i)
6.npm install<name>--save 将包安装的同时,将信息写入package.json中;
7.npm uninstall moudleName:卸载node模块
nrm 源管理器
在node界面中输入
npm config set registry https://registry.npm.taobao.org
即可将npm源修改成淘宝的源(服务器)
安装nrm,可将其安装到全局
指令npm i nrm -g
执行命令nrm ls 查看可选的源。当前源上有*符号
执行命令nrm use (taobao)源名,可切换所使用的源
nodemon:自动重启应用,并执行修改后的代码。
安装:npm i nodemon -g
使用:nodemon 文件名(不加文件名默认打开index.js)
Nodejs内置模块http  解析http协议,可以用来搭建服务器
// 加载了内置模块 http   解析http协议
let http = require('http');
// console.log(http)
语言方法
2533rox8rXB7NO
1o6OM传媒公司是做什么的
37872007/07/03 19:12:24
// 一旦有客户端请求服务器 ,监听的回调函数就会执行let server = http.createServer((req,res)=>{ console.log('服务器:收到了客户端的请求') req // 是http请求 console.log(req.url) // 参数 if(req.url === '/favicon.ico'){ res.end() return; } console.log('aaaa') res//服务器给客户端的响应 数据库 读取本地文件 res.write('aaa'); res.end('bbbb')})// 80 // 监听主机上的 端口server.listen(3000,'192.168.13.56',function(){ console.log('已经开始监听了,服务器启动成功了')})
首先是解析http  然后通过http.createServer((req,res)=>这里是服务器收到的客户端的请求)
其次 server.listen(端口号,地址,回调函数)用来监听服务器是否启动成功
nodejs的api
url
url.parse('接口地址'),查询url的信息将字符串转为对象
url.format(),将url对象转为url字符串
例let url = require('url');// /ap/login.html?usename=aa&passwordlet urlStr = 'https://sub.host.com:8080/login?username=1212&password=222'let obj = url.parse(urlStr)//url字符串 转为 对象console.log(obj.pathname)//接口console.log(obj.query)// username=1212&password=222console.log(obj.search)// ?username=1212&password=222// url对象转为 url字符串let str = url.format(obj)console.log(str)
Query String:用于实现url参数字符串与参数对象的相互转换。
例:let qs = require('querystring')// URL字符串转为js中对象let obj = qs.parse('username=zhangsan&password=123')//{ username: 'zhangsan', password: '123' }console.log(obj)let obj2 = { username: 'zhangsan', password: '123' }//username=zhangsan&password=123let str = qs.stringify(obj2)console.log(str)
Path
path模块提供了用于处理文件路径的工具
path.normalize()//正常化路径
path.join()//链接路径
path.resolve()//转化为绝对路径
path.extname()//获取当前路径下文件名的后缀
例:let path = require('path')// 正常化路径console.log(path.normalize('/ap/aa/..')) // 链接路径 \ap\admin\index.htmlconsole.log(path.join('/ap','admin','index.html'))//转化为绝对路径 D:\教学\zz2003\DAY16\代码\06.path.jsconsole.log(path.resolve('06.path.js'))// 获取url中的后缀 .htmlconsole.log(path.extname('http://aaaadfadf/login.html'))console.log(path.extname('/zxx.jpg'))
File System--node操作文件
用require('fs')来表示
1)stat() 获取文件的一些信息
2)mkdir() 新增目录的
3)writeFile(url,con,fc) 书写文件
4)appendFile(url,con,fc) 追加文件内容
5)读取文件的内容:readFile(url,fc)
6)列出目录的东西:readdir
7)重命名目录或文件:rename
8)删除目录与文件:rmdir(目录),unlink(文件) readdirSync(src) 同步读取目录信息
例:// fs// 处理电脑的文件的// fs 里的方法基本都是异步,也有同步let fs = require('fs');// 错误优先fs.stat('06-path.js',function(err,data){ // 回调函数的第一个参数 一般都是err 错误,操作成功err是null,操作失败err就有错误信息了 if(!err){ console.log('读取成功') // console.log(data) } else{ console.log('读取失败',err) } })console.log(2222)// async 异步// sync 同步let res = fs.statSync('06-path.js');// console.log(res) // 创建目录fs.mkdir('./js',function(err,data){ if(err){ console.log(err) return } console.log('创建成功')})// 创建文件,并写入内容// fs.writeFile('./js/index.js','console.log("hello nodejs")',function(err){// if(err){// console.log('创建失败')// }else{// console.log('创建成功')// }// })// 文件追加内容// fs.appendFile('./js/index.js','\n var a= 1;',function(){// console.log('appendFile') // })// 读取文件内容// fs.readFile('./js/index.js',function(err,data){// console.log(data.toString())// })// 读取文件夹 [ 'a.js', 'index.js' ]// fs.readdir('./js',function(err,data){// console.log(data)// })// 重命名// fs.rename('./js/a.js','./js/b.js',function(err,data){// console.log(err)// })// 删除文件// fs.unlink('./js/b.js',function(err){// console.log(err)// })// fs.unlinkSync// 删除目录// fs.rmdir('./js',function(err){// console.log(err)// })// fs.rmdirSync
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Node.js基础入门第五天
Node.js文件的写入(另一部分和读取类似,所以没写)
node
Node.js开发Web后台服务
Node.js开发之访问Redis数据库教程
nodejs核心技术
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服