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

var fs = require('fs');

var contents = fs.readFileSync('package.json').toString();
console.log(contents);

Non Blocking I/O

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

console.log('Hello world'); //在控制台中输出Hello World , 令人熟悉的console类

package.json

{
"name": "_01_HelloWorld",
"version": "0.0.0",
"description": "Hello World",
"main": "app.js",
"author": {
"name": "Rami Sayar",
"email": ""
}
}

打开CMD执行命令node app可以看到执行结果Hello World。

$ cd 01_HelloWorld
$ node app
Hello World

  

4.FileIO

打开源码文件夹03_FILEIO

app.js

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

var fs = require("fs");

// Read File
fs.createReadStream("package.json")
// Write File
.pipe(fs.createWriteStream("out-package.json"));

6.HelloWorldHttp

打开源码文件夹02_HelloWorldHttp

app.js

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

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

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窗口,这两个窗口将会互相通讯。

$ cd 05_HelloWorldTCP
$ node server

  

$ cd 05_HelloWorldTCP
$ node client

8.Requests

打开源码文件夹10_Requests

app.js

var request = require("request");

request("http://www.bing.com", function(error, response, body) {
console.log(body);
});

  

Nodejs in Visual Studio Code 02.学习Nodejs的更多相关文章

  1. crossplatform---Nodejs in Visual Studio Code 02.学习Nodejs

    1.开始 源码下载:https://github.com/sayar/NodeMVA 在线视频:https://mva.microsoft.com/en-US/training-courses/usi ...

  2. Nodejs in Visual Studio Code 07.学习Oracle

    1.开始 Node.js:https://nodejs.org OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTAL ...

  3. Nodejs in Visual Studio Code 03.学习Express

    1.开始 下载源码:https://github.com/sayar/NodeMVA Express组件:npm install express -g(全局安装) 2.ExpressRest 打开目录 ...

  4. 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 ...

  5. Nodejs in Visual Studio Code 11.前端工程优化

    1.开始 随着互联网技术的发展,企业应用里到处都是B/S设计,我有幸经历了很多项目有Asp.Net的,有Html/js的,有Silverlight的,有Flex的.很遗憾这些项目很少关注前端优化的问题 ...

  6. Nodejs in Visual Studio Code 10.IISNode

    1.开始 Nodejs in Visual Studio Code 08.IIS : http://www.cnblogs.com/mengkzhaoyun/p/5410185.html 参考此篇内容 ...

  7. Nodejs in Visual Studio Code 01.简单介绍Nodejs

    1.开始 作者自己:开发人员,Asp.Net , html / js , restful , memcached , oracle ,windows , iis 目标读者:供自己以后回顾 2.我看No ...

  8. Nodejs in Visual Studio Code 04.Swig模版

    1.开始 设置Node_Global:npm config set prefix "C:\Program Files\nodejs" Express组件:npm install e ...

  9. crossplatform---Nodejs in Visual Studio Code 07.学习Oracle

    1.开始 Node.js:https://nodejs.org OracleDB: https://github.com/oracle/node-oracledb/blob/master/INSTAL ...

随机推荐

  1. RuntimePermissions

    This sample shows runtime permissions available in Android M and above. Display the log on screen to ...

  2. OSX10.11 CocoaPods 升级总结

    本文不会讨论CocoaPods的各种使用技巧以及各种原理,只是简单记录一下在升级过程中遇到的问题,如果使用中有各种问题来欢迎交流. Podfile.loc 文件变化 前几天一个小伙更新了CocoaPo ...

  3. Ubuntu上glibc CVE-2015-7547漏洞的POC验证和修复

    Ubuntu上查看Glibc版本 $ldd --version ldd (Ubuntu GLIBC 2.21-0ubuntu4) 2.21 Ubuntu上查看使用Glibc的相关程序 sudo lso ...

  4. SPOJ 4053 - Card Sorting 最长不下降子序列

    我们的男主现在手中有n*c张牌,其中有c(<=4)种颜色,每种颜色有n(<=100)张,现在他要排序,首先把相同的颜色的牌放在一起,颜色相同的按照序号从小到大排序.现在他想要让牌的移动次数 ...

  5. (转)Css样式兼容IE6,IE7,FIREFOX的写法

    根据FF和IE对一些符号识别的差异,我们可以单独对FF以及IE定义样式,例子: 区别IE6与FF:          background:orange;*background:blue;   区别I ...

  6. markdown 简明语法

    今天同事聊到markdown用法 之前不怎么了解  先把网上的建明语法贴出来 以备后用. 基本符号 *,-,+ 3个符号效果都一样,这3个符号被称为 Markdown符号 空白行表示另起一个段落 `是 ...

  7. JavasScript基数排序

    基数排序   91, 46, 85, 15, 92, 35, 31, 22经过基数排序第一次扫描之后, 数字被分配到如下盒子中:Bin 0:Bin 1: 91, 31Bin 2: 92, 22Bin ...

  8. Ajax请求传递参数遇到的问题

    想写个同类型的,代码未测. 什么是WebAPI?我的理解是WebAPI+JQuery(前端)基本上能完成Web MVC的功能,即:这么理解吧,WebAPI相当于Web MVC的后台部分. 接下来直接上 ...

  9. L-value 和 R-value.

    An L-value is something that can appear on the left side of an equal sign, An R-value is something t ...

  10. WinForm窗体之间传值

    当程序需要将一个窗体中的一些信息传给另一个窗体并让其使用时,就需要用到这个知识点 方法一:通过接受参数的窗体的构造函数传值 例:现有Form1和Form2两个窗体,二者都包含一个文本框,Form1还包 ...