crossplatform---Nodejs in Visual Studio Code 02.学习Nodejs
1.开始
源码下载:https://github.com/sayar/NodeMVA
在线视频:https://mva.microsoft.com/en-US/training-courses/using-node-js-with-visual-studio-code-13920

2.事件驱动编程语言Nodejs
阻塞与非阻塞(Blocking I/O and Non Blocking I/O)
Blocking I/O
|
1
2
3
4
|
var fs = require('fs');var contents = fs.readFileSync('package.json').toString();console.log(contents); |
Non Blocking I/O
|
1
2
3
4
5
|
var fs = require('fs');fs.readFile('package.json', function (err, buf) { console.log(buf.toString());}); |
Nodejs与C#相反,readFileSync表示阻塞线程,而readFile不阻塞线程异步执行方法,调用完成后执行callback方法。
注:在c#中同步方法一般为object.action,同步方法会阻塞线程等待耗时操作执行完成,异步方法为object.actionAsyc,异步方法经常有个异步事件可以注册待异步方法执行结束后调用。
3.Hello World
打开源码文件夹01_HelloWorld,包含两个文件app.js和package.json
app.js
|
1
|
console.log('Hello world'); //在控制台中输出Hello World , 令人熟悉的console类 |
package.json
|
1
2
3
4
5
6
7
8
9
10
|
{ "name": "_01_HelloWorld", "version": "0.0.0", "description": "Hello World", "main": "app.js", "author": { "name": "Rami Sayar", "email": "" }} |
打开CMD执行命令node app可以看到执行结果Hello World。
|
1
2
3
|
$ cd 01_HelloWorld$ node appHello World |
4.FileIO
打开源码文件夹03_FILEIO
app.js
|
1
2
3
4
5
6
7
8
9
10
|
var fs = require('fs');//阻塞I/O toString 用起来很顺手,比学C#简单多了var contents = fs.readFileSync('package.json').toString();console.log(contents);//非阻塞I/Ofs.readFile('package.json', function (err, buf) { console.log(buf.toString());}); |
5.Streams
打开源码文件夹06_Streams
app.js
|
1
2
3
4
5
6
|
var fs = require("fs");// Read Filefs.createReadStream("package.json") // Write File .pipe(fs.createWriteStream("out-package.json")); |
6.HelloWorldHttp
打开源码文件夹02_HelloWorldHttp
app.js
|
1
2
3
4
5
6
7
8
9
10
|
var http = require('http');//处理http请求,返回200ok hello worldvar server = http.createServer(function (request, response) { response.writeHead(200, { "Content-Type": "text/plain" }); response.end("Hello World\n");});//侦听7000端口server.listen(7000); |

7.HelloWorldTCP
打开源码文件夹05_HelloWorldTCP
server.js
|
1
2
3
4
5
6
7
8
9
|
var net = require('net');// The handler argument is automatically set as a listener for the 'connection' eventvar server = net.createServer(function (socket) { console.log("Connection from " + socket.remoteAddress); socket.end("Hello World\n");});server.listen(7000, "127.0.0.1"); |
client.js
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
var net = require('net');var client = new net.Socket();client.connect(7000, "127.0.0.1");client.on('data', function (data) { console.log('Data: ' + data); client.destroy();});// Add a 'close' event handler for the client socketclient.on('close', function () { console.log('Connection closed');}); |
打开两个CMD窗口,这两个窗口将会互相通讯。
|
1
2
|
$ cd 05_HelloWorldTCP$ node server |
|
1
2
|
$ cd 05_HelloWorldTCP$ node client |

8.Requests
打开源码文件夹10_Requests
app.js
|
1
2
3
4
5
|
var request = require("request"); request("http://www.bing.com", function(error, response, body) { console.log(body);}); |
http://www.cnblogs.com/mengkzhaoyun/p/5355186.html
crossplatform---Nodejs in Visual Studio Code 02.学习Nodejs的更多相关文章
- Nodejs in Visual Studio Code 02.学习Nodejs
1.开始 源码下载:https://github.com/sayar/NodeMVA 在线视频:https://mva.microsoft.com/en-US/training-courses/usi ...
- Nodejs in Visual Studio Code 07.学习Oracle
1.开始 Node.js:https://nodejs.org OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTAL ...
- Nodejs in Visual Studio Code 03.学习Express
1.开始 下载源码:https://github.com/sayar/NodeMVA Express组件:npm install express -g(全局安装) 2.ExpressRest 打开目录 ...
- Nodejs in Visual Studio Code 14.IISNode与IIS7.x
1.开始 部署IISNode环境请参考:Nodejs in Visual Studio Code 08.IIS 部署Nodejs程序请参考:Nodejs in Visual Studio Code 1 ...
- Nodejs in Visual Studio Code 11.前端工程优化
1.开始 随着互联网技术的发展,企业应用里到处都是B/S设计,我有幸经历了很多项目有Asp.Net的,有Html/js的,有Silverlight的,有Flex的.很遗憾这些项目很少关注前端优化的问题 ...
- Nodejs in Visual Studio Code 10.IISNode
1.开始 Nodejs in Visual Studio Code 08.IIS : http://www.cnblogs.com/mengkzhaoyun/p/5410185.html 参考此篇内容 ...
- Nodejs in Visual Studio Code 01.简单介绍Nodejs
1.开始 作者自己:开发人员,Asp.Net , html / js , restful , memcached , oracle ,windows , iis 目标读者:供自己以后回顾 2.我看No ...
- Nodejs in Visual Studio Code 04.Swig模版
1.开始 设置Node_Global:npm config set prefix "C:\Program Files\nodejs" Express组件:npm install e ...
- crossplatform---Nodejs in Visual Studio Code 07.学习Oracle
1.开始 Node.js:https://nodejs.org OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTAL ...
随机推荐
- 无法顺利删除问题处理一则-ORA-00604和ORA-00942错误
1.以sys/oracle登录 2. -- Create tablecreate table ARGUMENT$( OBJ# NUMBER not null, PROCEDURE ...
- 【hdu5527】【2015ACM/ICPC亚洲区长春站 】Too Rich
题目链接: pid=5527">http://acm.hdu.edu.cn/showproblem.php?pid=5527 题意&题解: 感觉自己真是弱啊,自己想的贪心是错的 ...
- null与对象的复杂关系(typeof null的结果是object的原因)
原文 简书原文:https://www.jianshu.com/p/c1608452d056 前言 对象是 JavaScript 的基础.在 JavaScript 中一共有六种主要类型(术语是“语言类 ...
- putty-gns3
hcl-cloud用的就是这个putty http://forum.gns3.net/topic5016.html File comment: Compiled PuTTY 0.62 for wind ...
- [Node.js] Use Realm Object Database with Node.js
Realm is an ACID compliant object database. In this lesson, you will learn how to install Realm, def ...
- c# 读/写文件(各种格式)
最简单的: --------写 //content是要写入文本的字符串 //(@txtPath + @"\" + rid + ".txt");要被写入的TXT ...
- [Node.js] Take Screenshots of Multiple Dimensions for Responsive Sites using Nightmare
When developing responsive websites, you will constantly be resizing your browser to make sure your ...
- php实现表示数值的字符串(is_numeric($s))
php实现表示数值的字符串(is_numeric($s)) 一.总结 is_numeric($s) 二.php实现表示数值的字符串 题目描述 请实现一个函数用来判断字符串是否表示数值(包括整数和小数) ...
- js进阶正则表达式6转义字符(加\转义)(.符号)(|符号)
js进阶正则表达式6转义字符(加\转义)(.符号)(|符号) 一.总结 转义字符:{} () / $ # & * . ....... //3.特殊字符,都要加转义\ 点符号:var reg2= ...
- html5-4 HTML5超链接、URL地址和表格
html5-4 HTML5超链接.URL地址和表格 一.总结 一句话总结: 1.cellspace有什么用? 清除表格的单元格间距 26 <table border='1px' cellspac ...