简单回顾Javascript的基本用法和Node.js中的Web模块。
Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境
特点:事件驱动、异步非阻塞I/O模型、轻量、高效,跨平台。
JavaScript基本语法回顾
var
定义变量,console.log(变量名)
打印内容JS中数据类型有 字符串、对象(数组、函数、Object)、布尔、null、undefined、数值
if语句
1
2
3
4
5
6
7if(1){
...
}else if(2){
...
}else{
..
}for循环
1
for(1;1;1){}
while循环
1
while(1){}
do-while循环
1
2
3do{
...
}while(1)数组的声明
1
2
3
4
5//直接声明
var ary = [1,2,'a','b'];
//使用内置构造函数
var ary = new Array();循环打印数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17//for循环
for (var i=0;i<ary.length;i++){
console.log(a[i]);
}
//for-in循环
for(i in ary){
console.log(ary[i]);
}
//forEach循环
ary.forEach(function(a,b,c){
console.log(a); //值
console.log(b); //下标
console.log(c); //数组本身
})函数的声明:在JS中,函数自身的作用域在声明的地方,而不是调用的地方
1
2
3
4
5
6
7
8//直接声明
function f1(){}
//表达式声明(函数只能在声明后调用)
var f2 = function(){}
//内置构造函数(一般不用)
var f3 = new Function();对象的定义
1
2
3
4
5
6
7var obj1 = {
name:'brother',
age:69,
fun:function(){
...
};
};JS中先有对象后有类,直接定义变量后可以用
__proto__
方法查看该变量所属的类1
2var str = 'abc';
console.log(abc.__proto__)
Node.js创建简单的Web应用
JS代码运行方式:
1
2
3
4
5//在终端中使用node命令
node xxx.js
//REPL
>直接敲JS代码
Node.js应用由三部分组成:
- 引入required模块
- 创建服务器
- 接受请求与响应请求
引入require模块:使用
require
指令来载入http模块,并将实例化的HTTP赋值给变量http。1
var http = require("http")
创建服务器:服务器可以监听客户端的请求,类似于 Apache 、Nginx 等 HTTP 服务器。
我们使用 http.createServer() 方法创建服务器,并使用 listen 方法指定这个HTTP服务器监听的端口号: 8888 端口。 函数通过 request, response 参数来接收和响应数据。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15var http = require('http');
http.createServer(function (request, response) {
// 发送 HTTP 头部
// HTTP 状态值: 200 : OK
// 内容类型: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// 发送响应数据 "Hello World"
response.end('Hello World\n');
}).listen(8888);
// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');执行node.js应用:将上列代码保存于
test.js
中,在终端中执行:1
node test.js
一个最基本的HTTP服务器架构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35var http = require('http');
var fs = require('fs');
var url = require('url');
// 创建服务器
http.createServer( function (request, response) {
// 解析请求,包括文件名
var pathname = url.parse(request.url).pathname;
// 输出请求的文件名
console.log("Request for " + pathname + " received.");
// 从文件系统中读取请求的文件内容
fs.readFile(pathname.substr(1), function (err, data) {
if (err) {
console.log(err);
// HTTP 状态码: 404 : NOT FOUND
// Content Type: text/html
response.writeHead(404, {'Content-Type': 'text/html'});
}else{
// HTTP 状态码: 200 : OK
// Content Type: text/html
response.writeHead(200, {'Content-Type': 'text/html'});
// 响应文件内容
response.write(data.toString());
}
// 发送响应数据
response.end();
});
}).listen(8080);
// 控制台会输出以下信息
console.log('Server running at http://127.0.0.1:8080/');使用Node创建Web客户端(功能:读取并输出index.html中的数据)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25var http = require('http');
// 用于请求的选项
var options = {
host: 'localhost',
port: '8080',
path: '/index.html'
};
// 处理响应的回调函数
var callback = function(response){
// 不断更新数据
var body = '';
response.on('data', function(data) {
body += data;
});
response.on('end', function() {
// 数据接收完成
console.log(body);
});
}
// 向服务端发送请求
var req = http.request(options, callback);
req.end();
- 本文作者: Squidward
- 本文链接: http://www.squidward.xyz/2020/11/24/NodeJs-Web简单学习/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!