从零开始学习Node.js

 更新时间:2021年09月24日 11:10:51   作者:guizi0809  
这篇文章主要介绍了从零开始学习Node.js结合具体实例形式分析了使用方法与相关注意事项,需要的朋友可以参考下,希望能够给你带来帮助

url模块

1.parse 方法

// test02.js
import http from 'http'
import url from 'url'
const parseUrl = url.parse('https://www.baidu.com/news?name=诸葛亮&age=18#helloworld')
console.log(parseUrl)
http.createServer((req, res) => {
    res.writeHead(200, {'Content-Type': 'text/html;charset=utf-8'})
    res.write('你好, hello world!')
    res.end()
}).listen(3000)
console.log('My server is running at http://localhost:3000')

解析url地址,获得一个被解析的url详情对象,包含协议、域名、路径、端口、查询参数、哈希等信息。

第二个参数为boolean值,默认为false,传true会将query转为对象

const parseUrl = url.parse('https://www.baidu.com/news?name=诸葛亮&age=18#helloworld', true)
console.log(parseUrl)

2.format 方法

传入一个url信息对象(即parse方法返回的对象),返回一个具体的路径,该方法是parse方法的逆运用。

const formatUrl = url.format({
    protocol: 'https:',
    slashes: true,
    auth: null,
    host: 'www.baidu.com',
    port: null,
    hostname: 'www.baidu.com',
    hash: '#helloworld',
    search: '?name=诸葛亮&age=18',
    query: 'name=诸葛亮&age=18',
    pathname: '/news',
    path: '/news?name=诸葛亮&age=18',
    href: 'https://www.baidu.com/news?name=诸葛亮&age=18#helloworld'
})
console.log(formatUrl)	 // 输出 https://www.baidu.com/news?name=诸葛亮&age=18#helloworld

3.resolve 方法

拼接或替换次级路径

const result1 = url.resolve('https://www.baidu.com', 'news')
const result2 = url.resolve('https://www.baidu.com/home', '')
const result3 = url.resolve('https://www.baidu.com/home', 'about')
const result4 = url.resolve('https://www.baidu.com/home/index', 'about')
const result5 = url.resolve('https://www.baidu.com/home/index?name=诸葛亮', 'about/hello')
console.log(result1)
console.log(result2)
console.log(result3)
console.log(result4)
console.log(result5)

输出结果:

events模块(事件驱动)

1.引入event模块

2.创建一个eventEmitter实例

3.利用eventEmitter中的on方法和emit方法实现事件驱动,类似vue中的$on和$emit,即发布订阅模式

可以解决异步需求,如下:

import fs from 'fs'
import event from 'events'

const eventEmitter = new event.EventEmitter()

eventEmitter.on('events', data => {
    console.log('收到的数据', data.toString())
})

fs.readFile('static/index.html', (err, data) => {
    eventEmitter.emit('events', data)
})

path模块

import path from 'path'
// 获取后缀名
const extName = path.extname('index.html')     // .html

总结

本篇文章就到这里了,希望能够给你带来帮助,也希望您能够多多关注脚本之家的更多内容!

相关文章

  • 浅析 NodeJs 的几种文件路径

    浅析 NodeJs 的几种文件路径

    本篇文章主要介绍了浅析 NodeJs 的几种文件路径,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-06-06
  • npm install编译时报"Cannot read properties of null (reading ‘pickAlgorithm‘)"错误的解决办法

    npm install编译时报"Cannot read properties of null (r

    这篇文章主要给大家介绍了关于npm install编译时报“Cannot read properties of null (reading ‘pickAlgorithm‘)“错误的解决办法,文中将解决方法介绍的非常详细,需要的朋友可以参考下
    2022-07-07
  • node.js爬虫爬取拉勾网职位信息

    node.js爬虫爬取拉勾网职位信息

    本篇文章主要介绍了node.js爬虫爬取拉勾网职位信息的方法。具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03
  • Node.js学习之内置模块fs用法示例

    Node.js学习之内置模块fs用法示例

    这篇文章主要介绍了Node.js学习之内置模块fs用法,结合实例形式详细分析了node.js内置模块fs的基本功能、用法与相关操作注意事项,需要的朋友可以参考下
    2020-01-01
  • Node.js 中使用 async 函数的方法

    Node.js 中使用 async 函数的方法

    async是一个流程控制工具包,提供了直接而强大的异步功能。基于Javascript为Node.js设计,同时也可以直接在浏览器中使用。
    2017-11-11
  • 整理几个关键节点深入理解nodejs

    整理几个关键节点深入理解nodejs

    这篇文章主要介绍了整理几个关键节点深入理解nodejs,文章围绕主题展开详细的内容介绍,需要的小伙伴可以参考一下,需要的小伙伴可以参考一下
    2022-07-07
  • koa2使用ejs和nunjucks作为模板引擎的使用

    koa2使用ejs和nunjucks作为模板引擎的使用

    这篇文章主要介绍了koa2使用ejs和nunjucks作为模板引擎的使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-11-11
  • 使用Make构建Node.js网站项目

    使用Make构建Node.js网站项目

    这篇文章介绍了使用Make构建Node.js网站项目的方法,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-01-01
  • Node.js和Express简单入门介绍

    Node.js和Express简单入门介绍

    本篇文章主要介绍了Node.js和Express简单入门介绍,详细介绍如何用Node.js和Express搭建一个web服务器,有兴趣的可以了解一下。
    2017-03-03
  • Node.js中的缓冲与流模块详细介绍

    Node.js中的缓冲与流模块详细介绍

    这篇文章主要介绍了Node.js中的缓冲与流模块详细介绍,本文讲解了缓冲(buffer)模块、类:Buffer、写入缓冲区、复制缓冲区、流模块等内容,需要的朋友可以参考下
    2015-02-02

最新评论