1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# 下载地址 // 官网地址 https://nodejs.org/en/ // 国内镜像 http://pan.baidu.com/s/1kU5OCOB#path=%252Fpub%252Fnodejs # 安装 // 在Windows上安装时务必选择全部组件,包括勾选Add to Path // npm是Node.js的包管理工具(package manager) node -v //v6.5.0 npm -v //3.10.3 node //进入Node.js交互环境,退出按两下Ctrl+C # 测试 // test.js node test.js 'use strict'; console.log('hello world'); # Visual Studio Code // 官方网站 https://code.visualstudio.com // 国内镜像 http://pan.baidu.com/s/1kU5OCOB#path=%252Fpub%252Fnodejs // 安装(全部勾选) // 设置 // 右下角把CRLF改成LF(兼容linux系统) // 调试(左边第4个图案) 没有配置的情况下点右边过来第2个图案 // 然后设置json,下面2个附加直接删了,program的文件名改成要调试的文件名 // 点击开始箭头就能调试了,需要手动按结束(方块图案)中止 # sublime Node插件 // 下载地址 https://github.com/tanepiper/SublimeText-Nodejs // 解压后改名为Nodejs // sublime -> Perferences -> Browse Packages 打开插件文件夹 // 将Nodejs剪切进来 // 打开Nodejs.sublime-build文件修改配置 // "encoding": "cp1252" -> "encoding": "utf8" // "cmd": ["taskkill /F /IM node.exe & node", "$file"] -> "cmd": ["node", "$file"] // 打开Nodejs.sublime-settings文件修改配置 // node_command": false -> "node_command": "D:\\Program Files\\nodejs\\node.exe" // "npm_command": false -> "npm_command": "D:\\Program Files\\nodejs\\npm.cmd" // 然后就能用ctrl+B运行node代码了 # 模块 // node中一个文件就是一个模块,文件名就是它的模块名 // 不同模块的变量可以重名,用闭包来实现的(function(){xx.js})(); // hello.js 'use strict'; var s = 'Hello'; function greet(name) { console.log(s + ', ' + name + '!'); } // 把函数greet作为模块的输出暴露出去,这样其他模块就可以使用greet函数了 // 输出的变量可以是任意对象、函数、数组等 module.exports = greet; // main.js 'use strict'; // 引入hello模块 变量greet就是在hello.js中用module.exports = greet;输出的greet函数 // 记得写路径 如果直接写'hello' 依次在内置模块、全局模块和当前模块下查找hello.js var greet = require('./hello'); var s = 'Michael'; greet(s); // Hello, Michael! // exports输出(因为node中module.exports默认是一个空对象) module.exports = { //可以赋值,可以.语法 hello: hello, greet: greet }; exports.hello = hello; //只能.语法 exports.greet = greet; # 基本模块 // js中的全局变量是window node中也有个全局变量global // node交互界面 可以看到console的方法 > global.console // process(代表当前Node.js进程) // test.js // process.nextTick()将在下一轮事件循环中调用 process.nextTick(function () { console.log('nextTick callback!'); }); console.log('nextTick was set!'); // node test.js 根据输出发现,process.nextTick不是立刻执行而是等到下一次事件轮询 // nextTick was set! // nextTick callback! // Node.js进程本身的事件就由process对象来处理 // 程序即将退出时的回调函数 process.on('exit', function (code) { console.log('about to exit with code: ' + code); }); # 判断js执行环境是浏览器还是Node if (typeof(window) === 'undefined') { console.log('node.js'); } else { console.log('browser'); } # fs Node.js内置的file system // 正常读取时,err为null data为读取到的string // 读取错误时,err是一个错误对象 data为undefined // 这是node的标准回调函数,第一个参数:错误信息 第二个参数:结果 'use strict'; var fs = require('fs'); fs.readFile('./test.php', 'utf-8', function(err, data){ if (err) { console.log(err); } else { console.log(data); } }); // 不传入编码,读取的是二进制文件,回调函数将返回一个Buffer对象 'use strict'; var fs = require('fs'); fs.readFile('./1.png', function(err, data){ if (err) { console.log(err); } else { console.log(data); console.log(data.length + ' bytes'); } }); // Buffer -> String var text = data.toString(); // String -> Buffer var buf = new Buffer('text', 'utf-8'); // 同步读文件 'use strict'; var fs = require('fs'); try { var data = fs.readFileSync('test.php', 'utf-8'); console.log(data); } catch(e) { console.log(e); } // 写文件 回调函数只关心成功与否,所以只需要第一个参数 // 同样,也有同步写文件方法 writeFileSync fs.writeFile('test.php', 'if string then utf-8, if Buffer then bin', function (err) { if (err) { console.log(err); } else { console.log('ok'); } }); // 获取文件信息 同样,也有同步方法 statSync fs.stat('test.php', 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); } } }); |