Node.js的第一节课

初体验



Node.js自带 REPL —交互式解释器
R for Read
E for Eval
P for Print
L for Loop

基本

加载url模块

1
var url = require('url')

url.parse

1
var a = 'https://coding.net/user?name=matteokjh'
1
var a_URL = url.parse(a) //字符串格式化函数

返回值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
> a_URL
Url {
protocol: 'https:',
slashes: true,
auth: null,
host: 'coding.net',
port: null,
hostname: 'coding.net',
hash: null,
search: '?name=matteokjh',
query: 'name=matteokjh',
pathname: '/user/index.html',
path: '/user/index.html?name=matteokjh',
href: 'https://coding.net/user/index.html?name=matteokjh' }
>

其中


protocol:协议
slashes:暂时理解为双斜杠
host:主机
hostname:主机名(域名)
search:‘?’以及后面跟的字符串
query:search去掉问号
pathname:路径名
path:路径 + search
href:url全称


index.js

介绍基本格式

1
$ cat index.js

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
// index.js
// 引入模块,类似include
var url = require('url');
var http = require('http');
var PORT = 8088;
//创建服务器
//listen作用是监听当有请求访问localhost:8088的时候 会执行里面的函数
//函数作为参数是JS一大特征,是函数式编程的体现
http.createServer(function(req,res){
//解析请求
//使用url模块解析
var t_url = url.parse(req.url);
var pathname = t_url.pathname;
//输出一下
console.log("Request for "+ pathname + " received. ");
console.log(t_url);
//响应部分
res.writeHead(404,{'Content-type': 'text/html'});
res.write('No Page!');
res.end();//发送响应
}).listen(PORT)
console.log(`Server running at http://127.0.0.1:${PORT}/`);
// console.log("Server running at http://127.0.0.1:"+PORT+"/");

index.js(续)

文件读取

1
var fs = require('fs'); //File System

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if(pathname ==='/index.html' || pathname==='/'){
//index.html,重定向
var data = fs.readFileSync('index.html');//异步读取--readFile为同步
console.log(data.toString());//此处data为ASCII码格式
res.writeHead(200,{'Content-type':'text/html'});
res.write(data);
}else if(pathname.startsWith('/pages')){
//子页
var temp = pathname;
var pos = temp.lastIndexOf('/');
var filePath = temp.substring(pos+1);
var data = fs.readFileSync('pages/'+filePath);
res.writeHead(200,{'Content-type':'text/html'});
res.write(data);
}else{
res.writeHead(404,{'Content-type':'text/html'});
res.write("404 NotFound!");
}
res.end();//发送响应

index.js(终)

模板渲染,重定向

1
2
var ejs = require('ejs'); // Embedded JavaScript(嵌入式JavaScript),为第三方包
var querstring = require('querystring'); // 查询字符串,GET方法内

1
2
3
4
5
6
7
8
9
if(filePath === 'blog.html'){
//ejs实例
var data = fs.readFileSync('pages/'+filePath);
result = ejs.render(data.toString(),{
list: blogList
})//JSON对象
res.writeHead(200,{'Content-type':'text/html'});
res.write(result);
1
2
3
4
5
6
7
8
if(pathname ==='/addBlog'){
//按钮提交即刻渲染路由
var fontData = querstring.parse(t_url.query);
//title=aaa&username=admin
blogList.push(fontData.title);
res.writeHead(302,{//重定向
'Location': '/pages/blog.html'
})

html部分

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<body>
<p>This is blog.html</p>
<form action="/addBlog">
<textarea name="title"></textarea>
<button type="submit">添加</button>
</form>
<ul>
<!--ejs模板语法-->
<% list.forEach(function(e){ %>
<li><%= e %></li>
<% }); %>
</ul>
</body>



终了.