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
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的更多相关文章
- crossplatform---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 ...
随机推荐
- 第一篇:数据库需求与ER建模
前言 在数据库建设过程中,哪一步最重要?绝大多数资料会告诉你,是需求分析阶段.这一步的好坏甚至直接决定数据库项目的成败. 需求分析阶段,也被称为ER建模(entity-relationship mod ...
- Google开发规范
v0.2 - Last updated November 8, 2013 源自 Google's C++ coding style rev. 3.274 目录 由 DocToc生成 头文件 ...
- python学习之-成员信息增删改查
python学习之-成员信息增删改查 主要实现了成员信息的增加,修改,查询,和删除功能,写着玩玩,在写的过程中,遇到的问题,旧新成员信息数据的合并,手机号和邮箱的验证,#!/usr/bin/env p ...
- c#将Excel数据导入到数据库的实现代码(OleDb)
sing System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web ...
- 最全ASCLL码
结果 描述 实体编号 space ! exclamation mark ! " quotation mark " # number sign # $ dollar sign $ ...
- [!] CocoaPods was not able to update the `master` repo...
输入pod install之后出现: [!] CocoaPods was not able to update the `master` repo. If this is an unexpected ...
- centos node卸载
1.通过包管理工具 如果是通过包管理工具安装的话,那就和包管理工具卸载 yum remove nodejs npm -y 2.手动 如果是通过手动安装:官方下载后安装 进入到安装的路径 cd /opt ...
- 基于Jquery easyui 选中特定的tab并更新
获取选中的 Tab // 获取选中的 tab panel 和它的 tab 对象 var pp = $('#tt').tabs('getSelected'); var tab = pp.panel('o ...
- 你好,C++(10)这次的C++考试你过了没有?C++中表示逻辑判断的布尔数据类型
3.4 布尔类型 在日常生活中,我们除了需要使用int类型的变量表示216路公交车:需要使用float类型的变量表示西红柿3.5元一斤,有时候还需要表示一种数据,那就是逻辑状态: “这次的C++考试 ...
- Mysql 索引的基础(下)
如果需要存储大量的URL并需要根据URL进行搜索查找.如果使用B-Tree 来存储URL,存储的内容就会很大,因为URL本身都很长.正常情况下会有如下查询: SELECT id FROM url WH ...