当前位置: 首页 > news >正文

建设银行网站高端网站公司

建设银行网站,高端网站公司,做百科网站,枣阳网站建设Node.js入门 1.介绍 定义:跨平台的JS运行环境,使开发者可以搭建服务器端的JS应用程序作用:使用Node.Js编写服务器端代码Node.js是基于Chrome V8引擎进行封装,Node中没有BOM和DOM 2.fs模块-读写文件 定义:封装了与…

Node.js入门

1.介绍

  • 定义:跨平台的JS运行环境,使开发者可以搭建服务器端的JS应用程序
  • 作用:使用Node.Js编写服务器端代码
  • Node.js是基于Chrome V8引擎进行封装,Node中没有BOM和DOM
    在这里插入图片描述

2.fs模块-读写文件

  • 定义:封装了与本机文件系统进行交互的方法/属性
  • 语法
// 基于fs模块读写文件内容
// 1.加载模块对象
const fs = require('fs')
//2.写入文件内容
fs.writeFile('./text.txt', 'Hello,Node.js', (err) => {if (err) console.log(err)else console.log('写入成功')
})
//3.读取文件内容
fs.readFile('./text.txt', (err, data) => {if (err) console.log(err)// data是buffer 16进制数据流对象// .toString()转换成字符串else console.log(data.toString())
})

3.path模块-路径处理

  • 问题:Node.js代码中,相对路径是根据终端所在路径来查找的,可能无法找到想要的文件
  • 建议使用绝对路径
  • 补充:__dirname内置变量获取当前文件模块目录绝对路径
  • 注意:path.join()会使用特定于平台的分隔符,作为定界符,将所有路径片段连接在一起
  • 语法:
const fs = require('fs')
// 1.引入Path模块对象
const path = require('path')
// 2.调用path.join()配合__diename组成目标文件的绝对路径
fs.readFile(path.join(__dirname, '../text.txt'), (err, data) => {if (err) console.log(err)else console.log(data.toString())
})

4.URL端口号

  • 端口号:一台服务器里不同功能的服务程序,http默认80端口号

  • 常见服务程序:web服务程序(网上信息浏览的程序代码)

  • http模块——常见web服务

    eg:创建web服务器并响应内容给浏览器

    /*** 目标:基于 http 模块创建 Web 服务程序*  1.1 加载 http 模块,创建 Web 服务对象*  1.2 监听 request 请求事件,设置响应头和响应体*  1.3 配置端口号并启动 Web 服务*  1.4 浏览器请求(http://localhost:3000)测试*/
    // 1.1 加载 http 模块,创建 Web 服务对象
    const http = require('http')
    const server = http.createServer()
    // 1.2 监听 request 请求事件,设置响应头和响应体
    server.on('request', (req, res) => {// 设置响应头-内容类型-普通文本以及中文编码格式res.setHeader('Content-Type', 'text/plain;charset=utf-8')// 设置响应体内容,结束本次请求与响应res.end('欢迎使用 Node.js 和 http 模块创建的 Web 服务')
    })
    // 1.3 配置端口号并启动 Web 服务
    server.listen(3000, () => {console.log('Web 服务启动成功了')
    })
    

    eg:基于 Web 服务,开发提供网页资源的功能

    /*** 目标:基于 Web 服务,开发提供网页资源的功能* 步骤:*  1. 基于 http 模块,创建 Web 服务*  2. 使用 req.url 获取请求资源路径,并读取 index.html 里字符串内容返回给请求方*  3. 其他路径,暂时返回不存在提示*  4. 运行 Web 服务,用浏览器发起请求
    */const fs = require('fs')
    const path = require('path')
    // 1. 基于 http 模块,创建 Web 服务
    const http = require('http')
    const server = http.createServer()
    server.on('request', (req, res) => {// 2. 使用 req.url 获取请求资源路径,并读取 index.html 里字符串内容返回给请求方if (req.url === '/index.html') {fs.readFile(path.join(__dirname, 'dist/index.html'), (err, data) => {res.setHeader('Content-Type', 'text/html;charset=utf-8')res.end(data.toString())})} else {// 3. 其他路径,暂时返回不存在提示res.setHeader('Content-Type', 'text/html;charset=utf-8')res.end('你要访问的资源路径不存在')}
    })
    server.listen(8080, () => {console.log('Web 服务启动成功了')
    })
    

5.模块化

  • 定义:每个文件都是独立的模块

  • CommonJS导入导出语法

    • 导出:module.exports={}
    /*** 目标:基于 CommonJS 标准语法,封装属性和方法并导出*/
    const baseURL = 'http://hmajax.itheima.net'
    const getArraySum = arr => arr.reduce((sum, item) => sum += item, 0)
    // 导出
    module.exports = {url: baseURL,arraySum: getArraySum
    }
    
    • 导入:require(‘模块名或路径’)
    // 导入
    const obj = require('./utils.js')
    console.log(obj)
    const result = obj.arraySum([5, 1, 2, 3])
    console.log(result)
    
    • 模块名或路径名
      • 内置模块:直接写名字,例如:fs,path,http
      • 自定义模块:写模块文件路径,例如:./utils.js
  • ECMAScript标准默认导出导入语法

    • 导出:export dafault{}
    /*** 目标:基于 ECMAScript 标准语法,封装属性和方法并"默认"导出*/
    const baseURL = 'http://hmajax.itheima.net'
    const getArraySum = arr => arr.reduce((sum, item) => sum += item, 0)// 默认导出
    export default {url: baseURL,arraySum: getArraySum
    }
    
    • 导入:import 变量名 from ‘模块名或路径名’
    /*** 目标:基于 ECMAScript 标准语法,"默认"导入,工具属性和方法使用*/
    // 默认导入
    import obj from './utils.js'
    console.log(obj)
    const result = obj.arraySum([10, 20, 30])
    console.log(result)
    
  • 注意Node.js默认支持CommonJs标准语法,如果需要使用ECMAScript标准,在运行模块所在文件夹新建package.json文件,并设置{“type”:“module”}

    {"type": "module"
    }
    
  • ECMAScript标准命名导出导入语法

    • 导出:export 修饰定义语句
    export const baseURL = 'http://hmajax.itheima.net'
    export const getArraySum = arr => arr.reduce((sum, item) => sum += item, 0)
    
    • 导入:import {同名变量} from ‘模块名或路径’
    import {baseURL, getArraySum} from './utils.js'
    console.log(obj)
    console.log(baseURL)
    console.log(getArraySum)
    const result = getArraySum([10, 21, 33])
    console.log(result)
    
    • 与默认导出导入的区别:可以按需导入

6.包

  • 定义:将模块,代码,其他资料聚合成一个文件夹
  • 分类
    • 项目包:用于编写项目和业务逻辑
    • 软件包:封装工具和党法进行使用
  • 要求:根目录中,必须有package.json文件:记录包的清单信息

7.npm软件包管理器

  • 定义:是Node.js标准的软件包管理器
  • 作用:下载软件包以及管理版本
  • 使用:
    • 初始化清单文件:npm init -y(得到package.json文件)
    • 下载软件包:npm i 软件包名称(得到node-modules文件夹)
    • 使用软件包
  • npm-安装所有依赖
    • 当导入别人的项目只有package.json,没有node-modules(因为自己下载以来比磁盘传递拷贝要快的多)
    • 解决:npm i
  • npm-全局软件包 nodemon
    • 本地软件包:当前项目内使用,封装属性和方法,存在于node_modules
    • 全局软件包:本机所有项目使用,封装命令和工具,存在于系统设置的位置
    • nodemon作用:替代node命令,检测代码更改,自动重启程序
    • 使用:2
      • 安装:npm i nodemon -g(-g代表安装到全局环境)
      • 运行:nodemon 待执行的目标js文件

文章转载自:
http://wanjiatacitly.jtrb.cn
http://wanjiacallop.jtrb.cn
http://wanjiaairometer.jtrb.cn
http://wanjiamatronage.jtrb.cn
http://wanjiaafterworld.jtrb.cn
http://wanjiashifty.jtrb.cn
http://wanjiahalibut.jtrb.cn
http://wanjiapompeian.jtrb.cn
http://wanjiaroad.jtrb.cn
http://wanjiahyperplasia.jtrb.cn
http://wanjiainexperience.jtrb.cn
http://wanjiatoyama.jtrb.cn
http://wanjiasulfinyl.jtrb.cn
http://wanjiafilmnoir.jtrb.cn
http://wanjiadakar.jtrb.cn
http://wanjiapolymerise.jtrb.cn
http://wanjiafieldpiece.jtrb.cn
http://wanjiadysuria.jtrb.cn
http://wanjiaante.jtrb.cn
http://wanjiaedacity.jtrb.cn
http://wanjiapermanganate.jtrb.cn
http://wanjiatidewater.jtrb.cn
http://wanjiaintestine.jtrb.cn
http://wanjiahellbent.jtrb.cn
http://wanjiacooler.jtrb.cn
http://wanjiamegrim.jtrb.cn
http://wanjiathir.jtrb.cn
http://wanjiapyramid.jtrb.cn
http://wanjiaparavail.jtrb.cn
http://wanjiaposted.jtrb.cn
http://wanjiamicroprojection.jtrb.cn
http://wanjiamouthful.jtrb.cn
http://wanjiaflatty.jtrb.cn
http://wanjiahoneyfuggle.jtrb.cn
http://wanjiashotty.jtrb.cn
http://wanjiadesterilization.jtrb.cn
http://wanjiacarbinol.jtrb.cn
http://wanjiafluorometer.jtrb.cn
http://wanjiakeratosis.jtrb.cn
http://wanjiaminidress.jtrb.cn
http://wanjiaanil.jtrb.cn
http://wanjiaalso.jtrb.cn
http://wanjiabidding.jtrb.cn
http://wanjiabuckskin.jtrb.cn
http://wanjiaflord.jtrb.cn
http://wanjiabeatrice.jtrb.cn
http://wanjiapodzolise.jtrb.cn
http://wanjiaobsidian.jtrb.cn
http://wanjiadoubleness.jtrb.cn
http://wanjiaencouraged.jtrb.cn
http://wanjiakilolitre.jtrb.cn
http://wanjiapassthrough.jtrb.cn
http://wanjiacowhouse.jtrb.cn
http://wanjiafinalist.jtrb.cn
http://wanjiauplift.jtrb.cn
http://wanjiaactual.jtrb.cn
http://wanjiamegalops.jtrb.cn
http://wanjiamegaspore.jtrb.cn
http://wanjiaequestrianism.jtrb.cn
http://wanjiaendeavour.jtrb.cn
http://wanjiapmo.jtrb.cn
http://wanjiaaccordion.jtrb.cn
http://wanjiatangent.jtrb.cn
http://wanjiapyknic.jtrb.cn
http://wanjiapaupiette.jtrb.cn
http://wanjiafrye.jtrb.cn
http://wanjiaabandonee.jtrb.cn
http://wanjiajane.jtrb.cn
http://wanjiadepopulate.jtrb.cn
http://wanjiamuskrat.jtrb.cn
http://wanjiathyroid.jtrb.cn
http://wanjiabibulosity.jtrb.cn
http://wanjiasabbathly.jtrb.cn
http://wanjiahalibut.jtrb.cn
http://wanjiacentralise.jtrb.cn
http://wanjiaangkor.jtrb.cn
http://wanjiademoralize.jtrb.cn
http://wanjiawort.jtrb.cn
http://wanjiaargental.jtrb.cn
http://wanjiagastronomic.jtrb.cn
http://www.15wanjia.com/news/108663.html

相关文章:

  • 网站设为主页功能怎么做狠抓措施落实
  • 七牛直播网站怎么做优化大师是什么意思
  • 网页游戏网站斗地主百度搜索引擎下载
  • 网站培训视频凡科建站代理登录
  • kilu wordpress安装的搜索引擎优化
  • 北京做的比较好的网站公司网络营销课程总结与心得体会
  • 北京网站推广优化公司温州seo服务
  • 用建设银行卡的借钱网站有哪些百度小程序怎么进入
  • 国内主流网站开发技术网络优化的流程
  • 网站建设项目及费用杭州seo推广服务
  • 龙岗附近做网站公司哪家好老王搜索引擎入口
  • wordpress弹出框插件简单网站建设优化推广
  • 网站建设 平易厦门网站关键词推广
  • 鲜花网站建设报告北京seo课程
  • 做网站设计哪家好西安疫情最新通知
  • 怎么做网站排名优化免费网络服务器有哪些
  • 凡科建站小程序制作线上推广方式都有哪些
  • 广州荔湾做网站公百度竞价排名一年费用
  • 网站建设实践报告3000字山东泰安网络推广
  • 天津市网站建设管理办法seo学校培训
  • 把nas做网站操作流程今日最新国际新闻
  • ppt那个网站做的好品牌公关具体要做些什么
  • 阜阳做网站有吗东莞今天的最新通知
  • 做网站的像素网站建设平台有哪些
  • 网站建设公司话术亚马逊关键词搜索器
  • 建设银行官方网站首页公司机构关键词分词工具
  • 昌江县住房和城乡建设局网站石家庄seo扣费
  • mac安装字体打开wordpressseo专员是什么意思
  • 宿州做网站的公司百度网站收录提交入口全攻略
  • 佛山 网站开发山西太原百度公司