node.js中的querystring模块

  • querystring 模块提供了用于解析和格式化网址查询字符串的实用工具。 可以使用以下方式访问它:const querystring=require("querystring")
  • querystring.parse(str[, sep[, eq[, options]]]),将指定字符串解析为js对象,str参数为需要解析的字符串, sep分隔符1,eq分隔符2例如下列代码
  1. const qs = require('querystring')
  2. let string = 'name-wangyi#pass-123#sex-0'
  3. let obj = qs.parse(string, '#', '-')
  4. console.log(obj);

上述代码执行结果如下
node1

  • querystring.stringify(obj[, sep[, eq[, options]]]),将js对象序列化成字符串,例如下列代码
  1. querystring.stringify({ foo: 'bar', baz: ['qux', 'quux'], corge: '' });
  2. // 返回 'foo=bar&baz=qux&baz=quux&corge='
  3. querystring.stringify({ foo: 'bar', baz: 'qux' }, ';', ':');
  4. // 返回 'foo:bar;baz:qux'

node.js中的fs模块

  • fs 模块支持以标准 POSIX 函数建模的方式与文件系统进行交互。
  • 要使用基于 promise 的 API:
  1. import * as fs from 'fs/promises';
  • 要使用回调和同步的 API:
  1. import * as fs from 'fs';

例如下列代码 基于使用promise的api

  1. import { unlink } from 'fs/promises';
  2. try {
  3. await unlink('/tmp/hello');
  4. console.log('successfully deleted /tmp/hello');
  5. } catch (error) {
  6. console.error('there was an error:', error.message);
  7. }

node.js中的vm模块

  • vm 模块允许在 V8 虚拟机上下文中编译和运行代码。 vm 模块不是安全的机制。 不要用它来运行不受信任的代码。
    例如下列代码
  1. const vm = require('vm');
  2. const x = 1;
  3. const context = { x: 2 };
  4. vm.createContext(context); // 上下文隔离化对象。
  5. const code = 'x += 40; var y = 17;';
  6. // `x` 和 `y` 是上下文中的全局变量。
  7. // 最初,x 的值为 2,因为这是 context.x 的值。
  8. vm.runInContext(code, context);
  9. console.log(context.x); // 42
  10. console.log(context.y); // 17
  11. console.log(x); // 1; y 未定义。
  • vm.runInContext(code, contextifiedObject[, options])vm.runInContext() 方法编译 code,在 contextifiedObject 的上下文中运行它,然后返回结果。 运行代码无权访问本地作用域。 contextifiedObject 对象必须之前已经使用 vm.createContext() 方法上下文隔离化,例如下列代码
  1. const vm = require('vm');
  2. const contextObject = { globalVar: 1 };
  3. vm.createContext(contextObject);
  4. for (let i = 0; i < 10; ++i) {
  5. vm.runInContext('globalVar *= 2;', contextObject);
  6. }
  7. console.log(contextObject);
  8. // 打印: { globalVar: 1024 }

更多相关文章

  1. Android使用Application总结
  2. 禁止android显示状态栏
  3. android中widgets的简单实现
  4. Android(安卓)中各种XML文件的作用
  5. android经典开源代码集合
  6. 1、android源代码下载与跟踪
  7. 安卓,rebuild apk错误解决
  8. Android中重复执行动画bug
  9. android 设置无标题

随机推荐

  1. gin是什么意思?
  2. c语言中undeclared identifier是什么意思
  3. C++如何给二维数组初始化
  4. 什么是Go语言?Go语言的优缺点介绍
  5. 一个c程序的执行是从哪里开始到哪里结束
  6. devc++怎么改成中文
  7. 在c语言中引用数组元素时,其数组下标的数
  8. c++中=和==的区别有哪些?
  9. 在c程序中,注释语句只能位于一条语句的后
  10. c++中不能重载的运算符有哪些