http://www.cnblogs.com/mangoxin/p/5664615.html

https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/001434501504929883d11d84a1541c6907eefd792c0da51000

首次接触node,

赶紧把基础的又容易忘的简单的记下来

// fs 模块的常用方法,
var fs = require("fs"); fs.readFile("/home/pt/so", "'utf-8", function (err, data) { // 异步读取文件
// 异步读取文件 如果是文本类型,可以传一个编码类型
// 如果不传入编码,则返回一个 Buffer对象 一个包含零个或任意个字节的数组(注意和Array不同)
// err参数代表一个错误对象,data为undefined
// data 的一些常用方法
// data.toString(); // 转换成字符串 // 还可以反向转换 var buf = new Buffer(text, "utf-8")
// data.length // 返回字节数:单位是 bytes
// }) var file = fs.readFileSync("/home/pt/so", "utf-8"); // 如果同步读取文件发生错误,则需要用try...catch捕获该错误
// 写入文件
fs.writeFile("output.txt", data, function (err) {
// 第二个参数的文件内容
// 默认按UTF-8编码写入文本文件
// 如果传入的参数是Buffer,则写入的是二进制文件
})

如果需要获取文件的信息

var fs = require('fs');

fs.stat('sample.txt', function (err, stat) {
if (err) {
console.log(err);
} else {
// 是否是文件:
console.log('isFile: ' + stat.isFile());
// 是否是目录:
console.log('isDirectory: ' + stat.isDirectory());
if (stat.isFile()) {
// 文件大小:
console.log('size: ' + stat.size);
// 创建时间, Date对象:
console.log('birth time: ' + stat.birthtime);
// 修改时间, Date对象:
console.log('modified time: ' + stat.mtime);
}
}
});
fs.close(fd, callback) // 关闭文件
fs.ftruncate(fd, len, callback) // 截取文件
fs.unlink(path, callback) // 删除文件
fs.mkdir(path [, mode], callback) // 创建目录,mode 是目录权限 默认为0777
fs.readdir(path, callback) // 查看目录 回调函数带有两个参数err, files,err 为错误信息,files 为 目录下的文件数组列表
fs.rmdir(path, callback) // 删除目录
fs.exists(path, callback) // 判断路径是否存在 callback 参数 exists , 判断真假。
fs.appendFile(name, str, encode, callback) // name : 文件名 str : 添加的字段 encode : 设置编码 callback : 回调函数

url 模块

var url = require("url");
console.log(url.parse("http://user:pass@host.com:8080/path/to/file?query=string#hash"));
Url {
protocol: 'http:',
slashes: true,
auth: 'user:pass',
host: 'host.com:8080',
port: '8080',
hostname: 'host.com',
hash: '#hash',
search: '?query=string', // 多个的话 "?a=2&b=3&c=4"
query: 'query=string', // 多个的话 "a=2&b=3&c=4"
pathname: '/path/to/file', // 去除参数的路径
path: '/path/to/file?query=string', // 除域名外的路径,路由
href: 'http://user:pass@host.com:8080/path/to/file?query=string#hash' // 完整路径
}

path 模块

var path = require("path");
var workDir = path.resolve(".") // 解析当前路径
var filePath = path.join(workDir, "pul", "index.html") // 拼接路径

node 常用模块及方法fs,url,http,path的更多相关文章

  1. node常用模块---path

    path---用来提供文件路径和文件之间的处理的函数 node常用模块之path

  2. node常用模块汇总

    node常用模块汇总: 点击插件名字,查看使用文档 npm常用模块汇总 node常用模块汇总 gulp常用插件汇总 mkdirp:在node.js中像mkdir -p一样递归创建目录及其子目录

  3. Python编程-常用模块及方法

    常用模块介绍 一.time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行 ...

  4. nginx应用场景,特性,目录结构,常用模块,内置变量,URL和URI,http状态码,配置文件详解

    1.nginx介绍 1丶俄罗斯人开发的,开源www服务软件 2丶软件一共780K 3丶nginx本身是一款静态(html,js,css,jpg等)www软件 4丶静态小文件高并发,同时占用的资源很少, ...

  5. node 常用模块

    像在服务器上的操作,我们只要 require 引入的模块,只要不是 nodejs 中的模块,我们的下载环境都是开发环境 配置自动化:引用插件 nodemon 下载:npm i nodemon -g  ...

  6. python 常用模块及方法

    ******************** PY核心模块方法 ******************** os模块: os.remove()         删除文件 os.unlink()        ...

  7. python常用模块:sys、os、path、setting、random、shutil

    今日内容讲了3个常用模块 一.sys模块二.os模块三.os下path模块四.random模块五.shutil模块 一.sys模块 import sys #环境变量 print(sys.path) # ...

  8. 十、Node.js-url模块

    下面使用之前提到过的note交互模式(可以在cmd直接执行js代码)进行学习url模块 跳出note模式同样是Ctrl+C(两次) 学习url模块主要是要掌握url模块的方法: url.parse() ...

  9. npm常用模块汇总

    npm常用模块汇总: 点击插件名字,查看使用文档 npm常用模块汇总 node常用模块汇总 gulp常用插件汇总 npx 使用教程:npx使用教程 bable:bable这是JavaScript编译器 ...

随机推荐

  1. On-die termination for DDR

    本文转载自: https://blog.csdn.net/weixin_38233274/article/details/81016870 ODT是什么鬼?为什么要用ODT?在很多关于DDR3的博文和 ...

  2. pycharm 激活码

    http://www.cnblogs.com/itfat/p/9388829.html

  3. 使用nodeValue获取值与a标签默认跳转的冲突问题

    今天看javascript DOM编程艺术(第2版)发现这样一个例子: 效果图: 完整代码: <!DOCTYPE html> <html lang="en"> ...

  4. MySQL如何查询多少行,多少列

    查找表中有多少列: SELECT count(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA='cpm888' AND table_name ...

  5. mongodb mac

    ==> mongodb To have launchd start mongodb now and restart at login: brew services start mongodb O ...

  6. Could not create local repository at /home/yizhenn/.m、IDEA倒入maven项目无法导报问题

    问题描述: 用自己电脑新搭建环境,用idea倒入项目后发现无法倒入jar包,很少郁闷,折腾了很久,最终发现问题 settings文件中下面这个配置,需要是自己电脑的路径 <localReposi ...

  7. Android开发 静态static类与static方法持有Context是否导致内存泄露的疑问

    简述 在Android开发的过程中,难免会使用单例模式或者静态方法工具类.我们会让它们持有一些外部的Context或者View一般有以下几种情况: 单例模式,类的全局变量持有Context 或 Vie ...

  8. Android 开发 重写定位器类Timer与TimerTask

    class AttendanceTimer extends Timer { private static final int LOCATION = 0x01; private static final ...

  9. HTTPS的内网访问和访问外网

    https://launchpad.support.sap.com/#/notes/2461900 https://wiki.scn.sap.com/wiki/display/Security/Tro ...

  10. Python:笔记2

    [文件操作] 1.fileopen = open(file).readlines()    //type是list 2.filewrite = open(file,'w')     filewrite ...