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 app Hello 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/O fs.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 File fs.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 world var 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' event var 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 socket client.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 ...
随机推荐
- 【rlz02】二进制转十进制
Time Limit: 3 second Memory Limit: 2 MB 问题描述 输入一个二进制数,编程转换为十进制数. 整数部分不会超过65535,二进制的小数部分不会超过4位. Sampl ...
- idea+springboot+freemarker热部署(转)
今天在学习springboot集成freemarker模板引擎修改代码时,发现每次修改一次freemarker文件时,都必须重启下应用,浏览器刷新才能显示修改后的内容,这样效率太低,每次启动一次应用都 ...
- 微服务API模拟框架frock介绍
本文来源于我在InfoQ中文站翻译的文章,原文地址是:http://www.infoq.com/cn/news/2016/02/introducing-frock Urban Airship是一家帮助 ...
- JobService和JobScheduler机制在Android5.0以上保活
JobService和JobScheduler机制在Android5.0以上保活 我们知道在Android5.0之前,Android源代码还是有不小漏洞的,导致非常多不光明的手段来进行++保活++.但 ...
- 使用XX-Net永久访问真正的互联网
XX-Net基于GoAgent(代理软件),使用谷歌App Engine(GAE)代理服务器通过防火墙,是github上的一个开源项目. https://github.com/XX-net/XX-Ne ...
- TEMPDB
TEMPDB暴涨 阅读目录 前言 正文 原因 解决 补充 回到顶部 前言 tempdb暴增,造成磁盘空间不足,甚至影响业务运行. 回到顶部 正文 如图,tempdb log文件从7.4 ...
- Activity 调用Service的方法
一般来说,Activity调用Service 分为两种:进程内调用和进程间调用.进程内调用时比较常用的一种,在进程内调用中我们常常使用的是bindService来启动Service(关于这种启动方式的 ...
- php字符串转时间戳
PHP 提供了函数可以方便的将各种形式的日期转换为时间戳,该类函数主要是: strtotime():将任何英文文本的日期时间描述解析为时间戳. mktime():从日期取得时间戳. strtotime ...
- 某整形数组中除了两个单身整数外, 其余的整数都是成对出现的, 利用C/C++代码求出这两个单身整数。 要求: 时间复杂度o(n), 空间复杂度o(1)------某公司招聘试题
先看看这个题目:某整形数组中除了两个单身整数外, 其余的整数都是成对出现的, 利用C代码求出这两个单身整数. 要求: 时间复杂度o(n), 空间复杂度o(1). 我们先用最傻瓜的方式来做吧: #inc ...
- MySQL建立双向主备复制server配置方法
1.环境描写叙述 serverA(主) 192.85.1.175 serverB(从) 192.85.1.176 Mysql版本号:5.1.61 系统版本号:System OS:ubuntu 10.1 ...