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. 安装Stomp扩展时错误提示error: 'zend_class_entry' has no member named 'default_properties'

    在安装stomp扩展时, 有这样的提示 error: 'zend_class_entry' has no member named 'default_properties' 交待下安装上下文, sto ...

  2. python手机号码运营商归属测试

    #手机号码测试: def number_test(): while True: number = input('Please enter your phone number:') CN_mobile ...

  3. Python自动化之5种session类型

    Django中默认支持Session,其内部提供了5种类型的Session供开发者使用: 数据库(默认) 缓存 文件 缓存+数据库 加密cookie 1.数据库Session Django默认支持Se ...

  4. c# json数据解析——将字符串json格式数据转换成对象

    网络中数据传输经常是xml或者json,现在做的一个项目之前调其他系统接口都是返回的xml格式,刚刚遇到一个返回json格式数据的接口,通过例子由易到难总结一下处理过程,希望能帮到和我一样开始不会的朋 ...

  5. VI文件编辑操作说明

    vi Hello.c (回车后就进入,按i或a键开始编辑.要退出按ESC,进入中间模式,按冒号 :后面跟命令):wq (保存并退出):q!(退出不保存)

  6. struts2与spring整合问题,访问struts2链接时,spring会负责创建Action

    每次访问一次链接,spring会创建一个对象,并将链接所带的参数注入到Action的变量中(如何做到的呐) 因为: struts2的action每次访问都重新创建一个对象,那spring的ioc是怎么 ...

  7. iOS9 集成指纹解锁

    添加依赖库 LocalAuthentication.framework #import <LocalAuthentication/LocalAuthentication.h> // 头文件 ...

  8. WARN TaskSetManager: Lost task 0.0 in stage 0.0 (TID 0, worker1): java.lang.ClassNotFoundException: com.spark.firstApp.HelloSpark$$anonfun$2

    进行如下设置,解决报错信息. val conf = new SparkConf().setAppName("helloSpark").setMaster("spark:/ ...

  9. cocos2dx 3.2中的物理引擎初探(一)

    cocos2dx在设计之初就集成了两套物理引擎,它们是box2d和chipmunk.我目前使用的是最新版的cocos2dx 3.2.引擎中默认使用的是chipmunk,如果想要改使用box2d的话,需 ...

  10. Android动画 fillAfter和fillBefore

    fillBefore是指动画结束时画面停留在此动画的第一帧; fillAfter是指动画结束是画面停留在此动画的最后一帧. java代码设置如下: /*****动画结束时,停留在最后一帧******* ...