1 env start 搭建环境
1.1 how to install nodejs and how to run nodejs
1.2 how to use postman--nodejs---linux c
2 base knowledge 基础知识
2.1 解释执行
2.2 require("winston"); import lib
2.3 异步操作
3 json string json 串
3.1 json string 语法
3.2 json string parse实现
4 app.use
5 reset
6 code
1 env start 搭建环境
1.1 how to install nodejs and how to run nodejs
2). cd xxx
3). npm init ( enter next…next…next…)
4). npm install express --save
5). npm install body-parser --save
6). node server3.js
1.2 how to use postman--nodejs---linux c
2 base knowledge 基础知识
2.1 类似解释性语言,不是编译整个文件,而是run到哪个分支,再检查这个分支,只检查这个分支code。所以已经run的nodejs文件,中间可能存在问题,只是没走到那个分支,不检查而没有出错
2.2 require("winston"); //require lib 类似import c++的函数库
note:npm install winston //install lib 如果没有这个lib,则用npm install
2.3 异步操作:
错误做法:
fs.writeFile('./result.text',datanew,function(err) ;
dosomething(result.txt);
因为nodejs是异步操作,所以在writeFile没有结束的时候,就开始了dosomething(result.txt).
正确做法;
fs.writeFile('./result.text',datanew,function(err){
if(err) throw err;
console.log ("async read\n");
//等writeFile结束,进入函数体,执行dosomething(result.txt)
dosomething(result.txt);
});
3 json string json 串
3.1 json string 语法
json string: 两种括号,[]表示数组,{}表示对象。item 要放到" "中.每个item之间要用','分隔.最后一个不要加','。key : value形式.value 可以是对象也可以是数组,多个对象必须放到数组中。
json是可以递归的.
{
"message" : "client",
"UUID" : "28:cf:e9:07:83:af",
......
"media" : [
{
"type" : "audio",
"payloads": [
{
"name" : "SirenSAC",
......
}
],
"encryption": [
{
"tag": 1,
"cipherSuite": "AES_CM_128_HMAC_SHA1_80",
......
},
{
"tag": 2,
"cipherSuite": "AES_CM_128_HMAC_SHA1_32",
......
}
],
......
}
]
}
3.2
somebody implement json string parse :
setstring for basic
setcomplex for recusion,each item is other json string
one json string link other one with struct next;
4 app.use
app.use是中间件。层层解析,cookie,body ...每个app.use解析一层
var app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json for post jason string
app.use(bodyParser.json());
//1/localcandicate
app.get('/query/:id/:x', function(req, res) {
......
}
5 reset
rest 是一种协议,就是post,get,post, delete;用java,nodejs等都可以
rest 仅仅用于http协议。即postman与nodejs之间。不能用于和c 程序的socket通讯
reset 格式: http://10.220.214.164:8080/api/rest/mrc/前缀; conference是模块,sessionid是第几个,stop是功能
http://1.2.3.4:8080/api/rest/mrc/conference
http://1.2.3.4:8080/api/rest/mrc/conference/{sessionId}
http://1.2.3.4:8080/api/rest/mrc/conference/{sessionId}/stop
http://1.2.3.4:8080/api/rest/mrc/profile