亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

從零開始學(xué)習(xí)Node.js

 更新時間:2021年09月24日 11:10:51   作者:guizi0809  
這篇文章主要介紹了從零開始學(xué)習(xí)Node.js結(jié)合具體實例形式分析了使用方法與相關(guān)注意事項,需要的朋友可以參考下,希望能夠給你帶來幫助

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詳情對象,包含協(xié)議、域名、路徑、端口、查詢參數(shù)、哈希等信息。

第二個參數(shù)為boolean值,默認(rèn)為false,傳true會將query轉(zhuǎn)為對象

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)

輸出結(jié)果:

events模塊(事件驅(qū)動)

1.引入event模塊

2.創(chuàng)建一個eventEmitter實例

3.利用eventEmitter中的on方法和emit方法實現(xiàn)事件驅(qū)動,類似vue中的$on和$emit,即發(fā)布訂閱模式

可以解決異步需求,如下:

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

const eventEmitter = new event.EventEmitter()

eventEmitter.on('events', data => {
    console.log('收到的數(shù)據(jù)', 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

總結(jié)

本篇文章就到這里了,希望能夠給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!

相關(guān)文章

  • Node.js筆記之process模塊解讀

    Node.js筆記之process模塊解讀

    這篇文章主要介紹了Node.js process模塊解讀,process存在于全局對象上,不需要使用require()加載即可使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • 詳談nodejs異步編程

    詳談nodejs異步編程

    本文詳細(xì)介紹了node.js異步編程的分類以及異步編程存在的問題,非常的詳盡,非常細(xì)致,這里推薦給小伙伴。
    2014-12-12
  • 在node中如何使用 ES6

    在node中如何使用 ES6

    這篇文章主要介紹了在node中如何使用 ES6 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • 說說如何利用 Node.js 代理解決跨域問題

    說說如何利用 Node.js 代理解決跨域問題

    這篇文章主要介紹了Node.js代理解決跨域問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • node使用Mongoose類庫實現(xiàn)簡單的增刪改查

    node使用Mongoose類庫實現(xiàn)簡單的增刪改查

    Mongoose是在nodejs環(huán)境中對MongoDB數(shù)據(jù)庫操作的封裝,這篇文章主要介紹了node使用Mongoose類庫實現(xiàn)簡單的增刪改查,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-11-11
  • Npm?Module作用及使用一文全解

    Npm?Module作用及使用一文全解

    這篇文章主要介紹了Npm?Module作用及使用一文全解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-11-11
  • Node.js web 應(yīng)用如何封裝到Docker容器中

    Node.js web 應(yīng)用如何封裝到Docker容器中

    這篇文章主要介紹了Node.js web 應(yīng)用如何封裝到Docker容器中,幫助大家更好的學(xué)習(xí)node.js和使用docker容器,感興趣的朋友可以了解下
    2020-09-09
  • nodejs 實現(xiàn)釘釘ISV接入的加密解密方法

    nodejs 實現(xiàn)釘釘ISV接入的加密解密方法

    這篇文章主要介紹了nodejs 實現(xiàn)釘釘ISV接入的加密解密方法,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧,需要的朋友可以參考下
    2017-01-01
  • node.js 使用ejs模板引擎時后綴換成.html

    node.js 使用ejs模板引擎時后綴換成.html

    本文給大家分享一個nodejs的小技巧,將ejs模板引擎的模板后綴改成.html的使用方法,非常的簡單實用,這里推薦給大家。
    2015-04-04
  • NodeJS學(xué)習(xí)筆記之Connect中間件模塊(二)

    NodeJS學(xué)習(xí)筆記之Connect中間件模塊(二)

    本文續(xù)上文的內(nèi)容,介紹下nodejs中connect中間件的使用方式及用途,希望大家喜歡。
    2015-01-01

最新評論