nodejs
vscode
清空终端:cmd+K
inherits: 继承 chunk: 片段 pipe: 管道
require: 引入 request: 请求 response: 响应 listen: 监听
text/plain: 文本/纯 encode: 编码
var events = require('events'); //核心事件库
var util = require('util') //核心工具库 var fs = require('fs'); //文件系统库
var http = require('http'); //http模块
var url = require('url'); //url工具库
var querystring = require('querystring');
var express = require('express');
console.dir(req.params); //输出一个对象包含的内容
序列化(JSON.stringify(Obj)) 对象→string
反序列化(JSON.parse(objString)) string→对象
var Obj = { name: 'sunjia', job: 'programmer', age: 37 }; console.log(Obj); //{name: "sunjia", job: "programmer", age: 37} //对象 console.log(JSON.stringify(Obj)); //"{"name":"sunjia","job":"programmer","age":37}" //字符中(对象形式的),便于传输 console.log(JSON.parse('{"name":"sunjia","job":"programmer","age":37}')); console.log(JSON.parse(JSON.stringify(Obj))); //{name: "sunjia", job: "programmer", age: 37} //对象
fs异步: fs.readFile()、fs.writeFile()
//同步:sync
var fs = require('fs');
var readMe = fs.readFileSync("readMe.txt","utf8");
console.log(readMe);
console.log("finished");
// you read me!
// finished
// 异步方法都要加回调函数 var fs = require('fs'); var readMe = fs.readFile("readMe.txt","utf8", function(err,data) { console.log(data); }); console.log("finished"); // finished // you read me!
node事件
var events = require('events'); //引入核心事件库 var myEmitter = new events.EventEmitter(); //新建(实例化)一个事件对象 myEmitter.on('someEvent', function(message) { //监听事件 console.log(message); }) myEmitter.emit('someEvent', 'the event was emitted')//触发事件(根据事件名称) // the event was emitted
全局变量
// console.log(__dirname); // console.log(__filename); // require exports // console.log("hello world"); // setTimeout(() => { // console.log('2 秒开始执行') // }, 2000); // var time = 0; // var timer = setInterval(function() { // time += 2; // console.log(time + " seconds have passed"); // if(time > 5) { // clearInterval(timer) // } // }, 2000);
同一个变量:后边的值覆盖前边的值
var pi = 3.14; var adder = function(a) { return `the sum of 2 number is ${a+pi}`; } var pi = 1; console.log(adder(1)); // the sum of 2 number is 2
var pi = 3.14;
var adder = function(a) {
return `the sum of 2 number is ${a+pi}`;
}
var pi = 1;
console.log(adder("1"));
// the sum of 2 number is 11