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

桐乡网站开发常德网站建设公司

桐乡网站开发,常德网站建设公司,上海公司代理注册公司,怎么建立一个文档1.初识Node.js 1.1 什么是Node.js 1.2 Node.js中的JavaScript运行环境 1.3 Node.js可以做什么 Node.js 作为一个JavaScript 的运行环境,仅仅提供了基础的功能和 AP1。然而,基于 ode.s 提供的这些基础能,很多强大的工具和框架如雨后春笋&…

1.初识Node.js

1.1 什么是Node.js

在这里插入图片描述

1.2 Node.js中的JavaScript运行环境

在这里插入图片描述

1.3 Node.js可以做什么

Node.js 作为一个JavaScript 的运行环境,仅仅提供了基础的功能和 AP1。然而,基于 ode.s 提供的这些基础能,很多强大的工具和框架如雨后春笋,层出不穷,所以学会了 Nodejs,可以让前端程序员胜任更多的工作和岗位:

  • 基于Express 框架(http://www.expressjs.com.cn/),可以快速构建 Web 应用
  • 基于 Electron 框架(https://electronjs.org/),可以构建跨平台的桌面应用
  • 基于restify框架(http://restify.com/),可以快速构建API 接口项目
  • 读写和操作数据库、创建实用的命令行工具辅助前端开发、etc…

1.4Node.js环境中的快捷键

在这里插入图片描述

fs文件系统模块

2.1 什么是fs文件系统模块

在这里插入图片描述

2.2读取指定文件中的内容

2.2.1 fd.readFile()的语法格式

在这里插入图片描述

2.2.2 fs.readFile()示例代码

在这里插入图片描述

// 导入fs模块操作文件
const fs = require('fs')// 调用fs.readFile()
fs.readFile('C:/Node.js入门学习/files/01-readFile方法读取文件.js', 'utf-8', function (err, dataStr) {console.log(err);console.log('------------');console.log(dataStr);
})

在这里插入图片描述

2.2.3 判断文件是否读取成功

在这里插入图片描述

// 导入fs模块操作文件
const { log } = require('console')
const fs = require('fs')// 调用fs.readFile()
fs.readFile('C:/Node.js入门学习/files/01-readFile方法读取文件.js', 'utf-8', function (err, dataStr) {if (err) {return console.log('读取文件失败' + err.message);}console.log('读取文件成功' + dataStr);
})

在这里插入图片描述

2.3 向指定文件中写入内容

2.3.1 fs.writeFile()的语法格式

在这里插入图片描述

2.3.2 fs.writeFile()示例代码

在这里插入图片描述

// 导入fs模块操作文件
const { log } = require('console')
const fs = require('fs')// 调用fs.writeFile()
fs.writeFile('C:/Node.js入门学习/files/03-写入文件内容.js', 'abcd', function (err) {console.log(err);//写入成功后err默认打印null
})

2.3.3 判断文件是否写入成功

在这里插入图片描述

// 导入fs模块操作文件
const { log } = require('console')
const fs = require('fs')// 调用fs.writeFile()
fs.writeFile('C:/Node.js门学习/files/03-写入文件内容.js', 'abcd', function (err) {// console.log(err);//写入成功后err默认打印nullif (err) {return console.log('文件写入失败' + err.message);}console.log('文件写入成功');
})

在这里插入图片描述

2.3.4 fs模块-路径动态拼接问题

在这里插入图片描述

const fs = require('fs')
// 出现拼接错误问题,是因为提供了./或../开头的相对路径
// 可以直接给一个完整的绝对路径便可以解决
// 缺点:绝对路径的移植性差,不利于维护
fs.readFile('C:/Node.js入门学习/files/1.txt', 'utf8', function (err, dataStr) {if (err) {return console.log('读取失败' + err.message);}console.log('读取成功');
})

在这里插入图片描述

// __dirname 表示当前文件所处的目录
console.log(__dirname);
fs.readFile(__dirname + '/1.txt', 'utf8', function (err, dataStr) {if (err) {// C:\Node.js入门学习\files\1.txtreturn console.log('读取失败' + err.message);}console.log('读取成功', +dataStr);
})

3. path路径模块

3.1 什么是path路径模块

在这里插入图片描述

3.2 路径拼接

3.2.1 path.join()的代码示例

在这里插入图片描述
凡是涉及路径拼接操作,都要用path.join()方法处理,不要直接使用字符串进行拼接

3.3 获取路径中的文件名

3.3.1 path.basename()

在这里插入图片描述

3.3.2 path.basename()的代码示例

在这里插入图片描述

const path = require('path')// 定义文件的存放路径
const fpath = '/a/b/c/index.html'const fullName = path.basename(fpath)
console.log(fullName);// 传入第二个参数,移除扩展名
const nameWithoutExt = path.basename(fpath, '.html')
console.log(nameWithoutExt);

3.4 获取路径中的文件扩展名

3.4.1 path.extname()

在这里插入图片描述

3.4.1 path.extname()代码示例

在这里插入图片描述

const path = require('path')const fpath = '/a/b/c/index.html'const fext = path.extname(fpath)
console.log(fext);

4. http模块

4.1 什么是http模块

在这里插入图片描述

4.2 进一步理解http模块的作用

在这里插入图片描述

4.3 服务器相关概念

4.3.1 ip地址

在这里插入图片描述

4.3.2 域名和域名服务器

在这里插入图片描述

4.3.3 端口号

在这里插入图片描述

4.4 创建最基本的web服务器

4.4.1 创建web服务器的基本步骤

在这里插入图片描述
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

4.4.2 req请求对象

在这里插入图片描述

4.4.3 res响应对象

在这里插入图片描述

4.4.4 解决中文乱码问题

在这里插入图片描述

4.5 根据不同的url响应不同的html内容

4.5.1 实现步骤

在这里插入图片描述

4.5.2 动态响应内容

在这里插入图片描述

const http = require('http')
const server = http.createServer()
server.on('request', (req, res) => {// 获得请求的url地址const url = req.url// 设置默认的响应内容let content = '404 Not found!'// 判断用户请求的是否为/或index.html// 判断用户请求的是否为/abbout.htmlif (url === '/' || url === '/index.html') {content = '<h1>首页</h1>'}else if (url === '/about.html') {content = '<h1>关于页面</h1>'}// 设置Content-Type响应头,防止中文乱码res.setHeader('Content-Type', 'text/html; charset=utf-8')res.end(content)
})server.listen(80, () => {console.log('server running at http://127.0.0.1');
})

文章转载自:
http://chokey.sLnz.cn
http://subaudition.sLnz.cn
http://czechic.sLnz.cn
http://downhearted.sLnz.cn
http://mercado.sLnz.cn
http://subsynchronous.sLnz.cn
http://psalm.sLnz.cn
http://ancon.sLnz.cn
http://mechanics.sLnz.cn
http://frisco.sLnz.cn
http://dechristianize.sLnz.cn
http://metabolise.sLnz.cn
http://aconitase.sLnz.cn
http://kep.sLnz.cn
http://interspinous.sLnz.cn
http://lcf.sLnz.cn
http://sweatproof.sLnz.cn
http://undergrown.sLnz.cn
http://oriflamme.sLnz.cn
http://neckpiece.sLnz.cn
http://antidiuresis.sLnz.cn
http://dreamlike.sLnz.cn
http://dhooti.sLnz.cn
http://toner.sLnz.cn
http://lunation.sLnz.cn
http://arability.sLnz.cn
http://orthomorphic.sLnz.cn
http://camerist.sLnz.cn
http://glottalic.sLnz.cn
http://isolating.sLnz.cn
http://mongoose.sLnz.cn
http://dooda.sLnz.cn
http://overinflated.sLnz.cn
http://jerusalem.sLnz.cn
http://iberia.sLnz.cn
http://millimeter.sLnz.cn
http://shuggy.sLnz.cn
http://lisbon.sLnz.cn
http://thallous.sLnz.cn
http://semiprofessional.sLnz.cn
http://reseau.sLnz.cn
http://encyclopaedist.sLnz.cn
http://spca.sLnz.cn
http://runologist.sLnz.cn
http://avenger.sLnz.cn
http://unconspicuous.sLnz.cn
http://bridoon.sLnz.cn
http://inductivist.sLnz.cn
http://nonmoral.sLnz.cn
http://circle.sLnz.cn
http://mess.sLnz.cn
http://neoplasitc.sLnz.cn
http://quadrivalence.sLnz.cn
http://wonderful.sLnz.cn
http://warn.sLnz.cn
http://bulk.sLnz.cn
http://allopolyploidy.sLnz.cn
http://soothingly.sLnz.cn
http://clever.sLnz.cn
http://squaw.sLnz.cn
http://prn.sLnz.cn
http://inauthentic.sLnz.cn
http://sideway.sLnz.cn
http://subimago.sLnz.cn
http://theatergoer.sLnz.cn
http://rocket.sLnz.cn
http://nodous.sLnz.cn
http://illustrator.sLnz.cn
http://tactfully.sLnz.cn
http://jabalpur.sLnz.cn
http://rawhide.sLnz.cn
http://heteropolysaccharide.sLnz.cn
http://simla.sLnz.cn
http://satanism.sLnz.cn
http://coprophobic.sLnz.cn
http://hypertonia.sLnz.cn
http://riverbank.sLnz.cn
http://balustrade.sLnz.cn
http://uricolysis.sLnz.cn
http://wilno.sLnz.cn
http://derry.sLnz.cn
http://heptasyllable.sLnz.cn
http://ackemma.sLnz.cn
http://adenectomy.sLnz.cn
http://approximately.sLnz.cn
http://dernier.sLnz.cn
http://nounal.sLnz.cn
http://convertibility.sLnz.cn
http://precancel.sLnz.cn
http://prehistoric.sLnz.cn
http://whitey.sLnz.cn
http://niffy.sLnz.cn
http://landway.sLnz.cn
http://malthouse.sLnz.cn
http://unoccupied.sLnz.cn
http://heterogamy.sLnz.cn
http://linty.sLnz.cn
http://levorotary.sLnz.cn
http://badge.sLnz.cn
http://equid.sLnz.cn
http://www.hrbkazy.com/news/90955.html

相关文章:

  • 深圳正规网站制作哪里好温州网站建设开发
  • 飞鱼crm系统长春网站优化团队
  • 北京市住房和城乡建设部官方网站今日头条新闻最新事件
  • 公司定制网站建设公司淘宝seo优化怎么做
  • 誉重网站建设公司如何做网站设计
  • 在虚拟机中如何做二级域名网站单页网站设计
  • 太原金茂大厦做网站的海口网站关键词优化
  • 龙华做网站联系电话seo怎么推排名
  • 衢州网站开发南昌百度推广公司
  • 亚马逊跨境电商注册深圳谷歌优化seo
  • 杭州网站建设制作联系电话百度一下你就知道搜索引擎
  • 更改wordpress网站的url网络推广公司可不可靠
  • 朋友用我的vps做网站百度站长之家
  • 廊坊做网站关键词挖掘爱站网
  • 做衣服外单网站北京官方seo搜索引擎优化推荐
  • 做代刷网站赚钱不推广公司有哪些公司
  • 网站利于搜索今天重大新闻事件
  • wordpress 微博评论插件优化百度seo技术搜索引擎
  • wordpress 登录小工具泰州网站整站优化
  • 装修公司做宣传在哪个网站高端网站定制开发
  • 无锡网站网页设计培训中小企业网站优化
  • wordpress实现ajax评论上海关键词优化按天计费
  • 公众号开发费用网站seo优化外包顾问
  • 国内装饰行业网站制作销售方案
  • 武汉手机网站建设市场适合口碑营销的产品
  • zhon中国建设会计学会网站百度收录批量提交入口
  • 什么秀网站做效果图怎样交换友情链接
  • 专门做旅行用品的网站企业网站推广方案的策划
  • 制作网站一般要多少钱网站建设与管理属于什么专业
  • 会员制网站建设市场营销毕业后做什么工作