SERVER
// 导入常用的三个内置模块
const http = require('http');
const fs = require('fs');
const url = require('url'); //-> URL模块中提供一个方法 url.parse() 用于解析url地址
// 1.HTTP
//->http.createServer:创建一个服务
// ->server.listen:为这个服务监听一个端口888 端口范围:0~65535
var server = http.createServer(function (request, response) {
// 当客户端向当前服务(端口为888)发送一个请求,并且当前服务已经成功接收到这个请求后执行这个回调函数
// 发送请求方式: 1.http://localhost:端口号/ 2. http://本机IP地址:端口号/
// request(请求):存放的是所有客户端的请求信息,包含客户端通过问号传参的方式传递的数据
// response(响应):提供了向客户端返回内容与数据的方法
// console.log(request.url);
// request.url:存放的是客户端请求的文件资源的目录和名称已经传递给服务器的数据
// 客户端请求的完整地址为:http://localhost:888/index.html?name=fang&age=22 通过request.url获取到的为 /index.html?name=fang&age=22
var urlObj = url.parse(request.url, true);
var pathname = urlObj.pathname; //请求资源路径+名称
console.log(pathname);
var query = urlObj.query; //客户端传递来的数据(键值对形式)
if (pathname === '/server1.html') {
// 根据请求的URL地址(主要是地址中的pathname)获取到对应资源的源代码
//fs.readFileSync([path+name],[encode]):同步读取指定文件中的内容(同步读取:文件内容读取完成才执行后续操作)
var con = fs.readFileSync('./server1.html', 'utf8');
response.write(con); //向客户端返回内容
response.end(); //告诉服务器响应结束(必须写)
}
});
server.listen(666, function () {
// 当服务创建成功,且端口号也监听成功后执行
console.log('server iscreate success! listening on 666 port! ');
});
URL
const url = require('url');
var u = 'http://localhost:888/index.html?name=fang&age=22#fang';
// console.log(url.parse(u));//解析url地址 输出一个对象
/*
Url {
protocol: 'http:', //传输协议
slashes: true,
auth: null,
host: 'localhost:888', //域名+端口号
port: '888', //端口号
hostname: 'localhost', //域名
hash: #fang, //hash值
search: '?name=fang&age=22', //问号加传递来的数据
query: 'name=fang&age=22', //传递来的数据
pathname: '/index.html', //请求文件的路径及名称
path: '/index.html?name=fang&age=22', //路径+名称+数据
href: 'http://localhost:888/index.html?name=fang&age=22' //完整请求地址
}
*/
console.log(url.parse(u, true));//解析url地址 输出一个对象 加true后query部分变成键值对形式 所以平时这样使用更易操作
/*
Url {
。。。
query: { name: 'fang', age: '22' },
。。。
}
*/
QUERYSTRING
const querystring = require('querystring');
var data = {
protocol: 'https:',
slashes: true,
auth: null,
host: 'www.ibm.com:443',
port: '443',
hostname: 'www.ibm.com',
hash: '#name',
search: '?name=fang&sex=man',
query: 'name=fang&sex=man',
pathname: '/developerworks/cn/web/wa-use-javascript-closures-efficiently/index.html',
path: '/developerworks/cn/web/wa-use-javascript-closures-efficiently/index.html?name=fang&sex=man',
href: 'https://www.ibm.com:443/developerworks/cn/web/wa-use-javascript-closures-efficiently/index.html?name=fang&sex=man#name'
};
var q = querystring.stringify(data); //将url对象转为url字符串拼接格式 输出内容需要进行地址解码
/*
protocol=https%3A&slashes=true&auth=&host=www.ibm.com%3A443&port=443&hostname=www.ibm.com&hash=%23name&search=%3Fname%3Dfang%26sex%3Dman&query=name%3Dfang%26sex%3Dman&pathname=%2Fdeveloperworks%2Fcn%2Fweb%2Fwa-use-javascript-closures-efficiently%2Findex.html&path=%2Fdeveloperworks%2Fcn%2Fweb%2Fwa-use-javascript-closures-efficiently%2Findex.html%3Fname%3Dfang%26sex%3Dman&href=https%3A%2F%2Fwww.ibm.com%3A443%2Fdeveloperworks%2Fcn%2Fweb%2Fwa-use-javascript-closures-efficiently%2Findex.html%3Fname%3Dfang%26sex%3Dman%23name
*/
// q = querystring.unescape(q); //对url字符串地址进行解码 编码为:querystring.escape();
/*
protocol=https:&slashes=true&auth=&host=www.ibm.com:443&port=443&hostname=www.ibm.com&hash=#name&search=?name=fang&sex=man&query=name=fang&sex=man&pathname=/developerworks/cn/web/wa-use-javascript-closures-efficiently/index.html&path=/developerworks/cn/web/wa-use-javascript-closures-efficiently/index.html?name=fang&sex=man&href=https://www.ibm.com:443/developerworks/cn/web/wa-use-javascript-closures-efficiently/index.html?name=fang&sex=man#name
*/
q = querystring.parse(q); //将完整地址转为对象格式
/**
{ protocol: 'https:',
slashes: 'true',
auth: '',
host: 'www.ibm.com:443',
port: '443',
hostname: 'www.ibm.com',
hash: '#name',
search: '?name=fang&sex=man',
query: 'name=fang&sex=man',
pathname: '/developerworks/cn/web/wa-use-javascript-closures-efficiently/index.html',
path: '/developerworks/cn/web/wa-use-javascript-closures-efficiently/index.html?name=fang&sex=man',
href: 'https://www.ibm.com:443/developerworks/cn/web/wa-use-javascript-closures-efficiently/index.html?name=fang&sex=man#name' }
*/
console.log(q);
APPENDFILE
var fs = require('fs');
var data = `this is append data from `+__filename;
fs.appendFile('./test1/new1.js', data, (err) => {
if (err) throw err;
});
RENAME
const fs = require('fs');
const path = require('path');
files = fs.readdirSync('./test1');
// console.log(files);
for (var i = 0; i <files.length; i++) {
var oldpath = path.join(__dirname, 'test1', files[i]);
var newpath = path.join(__dirname, 'test1', files[i].replace(/test/, 'new'));
console.log(newpath);
fs.rename(oldpath, newpath, (err) => {
if (err) throw err;
});
}
CHMODSYNC
const fs = require('fs');
fs.chmodSync('./test1/new1.js', 0o444);
// Number Description
// 7 read, write, and execute
// 6 read and write
// 5 read and execute
// 4 read only
// 3 write and execute
// 2 write only
// 1 execute only
// 0 no permission
ACCESS
const fs = require('fs');
// fs.access('./test1/new1.js',fs.constants.F_OK, (err) => {
// console.log(`${'./test1/new1.js'}${err ? '不存在' :'存在'}`);
// });
// fs.access('./test1/new1.js', fs.constants.W_OK, (err) => {
// console.log(`${'./test1/new1.js'}${err?'不可写':'可写'}`);
// });
// fs.access('./test1/new1.js', fs.constants.R_OK, (err) => {
// console.log(`${'./test1/new1.js'}${err?'不可读':'可读'}`);
// });
// fs.access('./test1/new1.js', fs.constants.E_OK, (err) => {
// console.log(`${'./test1/new1.js'}${err?'不可执行':'可执行'}`);
// });
fs.access('./test1/new1.js', fs.constants.F_OK, (err) => {
err ? console.log('文件不存在') : fs.access('./test1/new1.js', fs.constants.W_OK, (err) => {
err ? fs.chmodSync('./test1/new1.js', 0o765, (err) => {
if (err) throw err;
// 备注部分执行不到
// fs.appendFile('./test1/new1.js', ' 源文件不可写,以修改权限,写入成功···', (err) => {
// if (err) throw err;
// });
}) :
fs.appendFile('./test1/new1.js', ' 源文件可写,写入成功···', (err) => {
if (err) throw err;
});
})
});
CRYPTO
const crypto = require('crypto');
const secret = 'abcdefg';
const hash = crypto.createHmac('sha256', secret)
.update('I love cupcakes')
.digest('hex');
console.log(hash);