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. 团队第六次 # scrum meeting

    github 本此会议项目由PM召开,召开时间为4-10日晚上9点 召开时长20分钟 任务表格 袁勤 负责协调前后端 https://github.com/buaa-2016/phyweb/issue ...

  2. jmeter多sql查询

    背景:实现多条sql语句,取多个值的情况 步骤:1.JDBC Connection Configuration配置添加?allowMultiQueries=true 2.增加sql语句 ,查询或修改语 ...

  3. postgresql 创建gin索引

    1.创建gin类型的索引 postgresql 创建gin索引遇到的问题:1.ERROR: operator class "gin_trgm_ops" does not exist ...

  4. C# xml 读xml、写xml、Xpath、Xml to Linq、xml添加节点 xml修改节点

    #region XDocument //创建XDocument XDocument xdoc2 = new XDocument(); XElement xel1= new XElement(" ...

  5. ExcelPackage 读取、导出excel

    private static string GetString(object obj) { try { return obj.ToString(); } catch (Exception ex) { ...

  6. py库:threading

    https://www.youtube.com/watch?v=DnTn3Yx-Nvg  join功能: import threading import time def thread_job2(): ...

  7. 常见java日志系统的搭配详解:关于slf4j log4j log4j2 logback jul jcl commons-logging jdk-logging

    先看一张图: 是不是有点晕, 晕就对了.这个仅仅是 slf4j 的情况,实际上, 我们不仅要接触到 slf4j ,有时候还会接触其他的日志系统.且看下文分解. 1 直接使用各个日志系统 1.1 直接使 ...

  8. Linux 打包压缩与搜索命令

    1.tar 用于对文件进行打包压缩或解压,格式为tar[选项][文件],-f参数必须放到参数最后一位 tar -czvf etc.tar.gz /etc tar参数及作用 参数 作用 -c 创建压缩文 ...

  9. 字符型转换为字符串ToString

    字符型转换为字符串 // C 货币 2.5.ToString("C"); // ¥2.50 // D 10进制数 25.ToString("D5"); // 2 ...

  10. ajax发送列表(traditional, serialize )

    $(function () { $('#add_submit_ajax').click(function () { $.ajax({ url:'/addapp/', data:$('#add_app' ...