常用内置模块-06学习

常用内置模块学习

先安装log4js
npm install log4js -D
1.url

// 常用内置模块
const log4js = require('log4js')

log4js.configure({
  appenders: { cheese: { type: 'file', filename: 'cheese.log' } },
  categories: { default: { appenders: ['cheese'], level: 'error' } }
})
var logger = log4js.getLogger('cheese')
logger.level = 'debug'

const url = require('url')

const urlString = 'https://www.baidu.com:443/path/index.html?id=2#tag=3'

logger.debug(url.parse(urlString))

const log4js = require('log4js')
const url = require('url')
log4js.configure({
  appenders: { cheese: { type: 'file', filename: 'cheese.log' } },
  categories: { default: { appenders: ['cheese'], level: 'error' } }
})
var logger = log4js.getLogger('cheese')
logger.level = 'debug'
const obj = {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com:443',
  port: '443',
  hostname: 'www.baidu.com',
  hash: '#tag=3',
  search: '?id=2',
  query: 'id=2',
  pathname: '/path/index.html',
  path: '/path/index.html?id=2',
  href: 'https://www.baidu.com:443/path/index.html?id=2#tag=3'
}

logger.debug(url.format(obj))

日志打印输出:

[2021-03-16T21:28:53.242] [DEBUG] cheese - Url {
  protocol: 'https:',
  slashes: true,
  auth: null,
  host: 'www.baidu.com:443',
  port: '443',
  hostname: 'www.baidu.com',
  hash: '#tag=3',
  search: '?id=2',
  query: 'id=2',
  pathname: '/path/index.html',
  path: '/path/index.html?id=2',
  href: 'https://www.baidu.com:443/path/index.html?id=2#tag=3' }
[2021-03-16T21:32:08.562] [DEBUG] cheese - https://www.baidu.com:443/path/index.html?id=2#tag=3

2.resolve

// 常用内置模块
const log4js = require('log4js')

log4js.configure({
  appenders: { cheese: { type: 'file', filename: 'cheese.log' } },
  categories: { default: { appenders: ['cheese'], level: 'error' } }
})
var logger = log4js.getLogger('cheese')
logger.level = 'debug'

const url = require('url')

const urlString = 'https://www.baidu.com:443/path/index.html?id=2#tag=3'

logger.debug(url.resolve('http://www.baidu.com/a', '../'))
logger.debug(url.resolve('http://www.baidu.com/a', '/b'))

[2021-03-16T21:38:40.789] [DEBUG] cheese - http://www.baidu.com/
[2021-03-16T21:38:40.796] [DEBUG] cheese - http://www.baidu.com/b

const urlParams = new URLSearchParams(url.parse(urlString).search)
// logger.debug(urlParams)
logger.debug(urlParams.get('id'))

[2021-03-16T21:47:25.479] [DEBUG] cheese - 2
上一篇:app hellocharts 柱状图


下一篇:node.js实现图片上传