Node.js基本模块http
Node.js开发的目的就是为了用JavaScript编写Web服务器程序。因
为JavaScript实际上已经统治了浏览器端的脚本,其优势就是有世界上
数量最多的前端开发人员。如果已经掌握了JavaScript前端开发,再学习一下如何将JavaScript应用在后端开发,就是名副其实的全栈了。
要理解Web服务器程序的工作原理,首先,我们要对
http协议有基本的了解。如果你对
http协议不太熟悉,先看一看
http协议简介。
要开发
http服务器程序,从头处理TCP连接,解析
http是不现实的。这些工作实际上已经由Node.js
自带的
http模块完成了。应用程序并不直接和
http协议打交道,而是操作
http模块提供的
request和response对象。
request对象封装了
http请求,我们
调用request对象的
属性和
方法就可以拿到所有
http请求的信息;
response对象封装了
http响应,我们操作response对象的
方法,就可以把
http响应返回给浏览器。
用Node.js实现
一个http服务器程序非常简单。我们来实现
一个最简单的Web程序
Hello.js,它对于所有请求,都返回
Hello World!:
'use Strict';
// 导入http模块:
var http = require('http');
// 创建http server,并传入回调函数:
var server = http.createServer(function (request,responsE) {
// 回调函数接收request和response对象,// 获得http请求的method和url:
console.log(request.method + ': ' + request.url);
// 将http响应200写入response,同时设置Content-Type: text/html:
response.writeHead(200,{'Content-Type': 'text/html'});
// 将http响应的HTML内容写入response:
response.end('<h1>Hello World!</h1>');
});
// 让服务器监听8080端口:
server.listen(8080);
console.log('Server is running at http://127.0.0.1:8080/');
在命令
提示符下运行该程序,可以看到以下
输出:
$ node
Hello.js
Server is running at
http://127.0.0.1:8080/
不要@L_
616_15@命令
提示符,直接打开浏览器输入
http://localhost:8080,即可看到服务器响应的
内容:
http-
Hello-sample
同时,在命令
提示符窗口,可以看到程序打印的请求信息:
GET: /
GET: /favicon.ico
这就是我们编写的第
一个http服务器程序!
让我们继续扩展一下上面的Web程序。我们可以设定
一个目录,然后让Web程序变成
一个文件服务器。要实现这一点,我们只需要解析
request.url中的路径,
然后在本地找到对应的
文件,把
文件内容发送出去就可以了。
解析URL
需要用到Node.js提供的url模块,它使用起来非常简单,通过parse()将
一个字符串解析为
一个Url对象:
'use Strict';
var url = require('url');
console.log(url.parse('http://user:pass@host.com:8080/path/to/file?query=String#hash'));
结果如下:
Url
{
protocol: '
http:',
slashes: true,
auth: 'user:pass',
host: 'host.com:8080',
port: '8080',
hostname: 'host.com',
hash: '#hash',
search: '?query=
String',
query: 'query=
String',
pathname: '/path/to/file',
path: '/path/to/file?query=
String',
href: '
http://user:pass@host.com:8080/path/to/file?query=
String#hash' }
处理本地
文件目录需要
使用Node.js提供的path模块,它可以方
便地构造目录:
'use
Strict';
var path =
require('path'
);
// 解析当前目录:
var workDir = path.resolve('.'
); // '/Users/michael'
// 组合完整的
文件路径:当前目录+'pub'+'index.html':
var filePath = path.join(workDir,'pub','index.html'
);
// '/Users/michael/pub/index.html'
使用path模块可以正确处理操作系统相关的
文件路径。在Windows系统下,返回的路径类似于C:\Users\michael\static\index.html,这样,我们就不关心怎么拼接路径了。
最后,我们实现
一个文件服务器file_server.js:
'use Strict';
var
fs = require('fs'),url = require('url'),path = require('path'),http = require('http');
// 从命令行参数获取root目录,默认是当前目录:
var root = path.resolve(process.argv[2] || '.');
console.log('Static root dir: ' + root);
// 创建服务器:
var server = http.createServer(function (request,responsE) {
// 获得URL的path,类似 '/css/bootstrap.css':
var pathname = url.parse(request.url).pathname;
// 获得对应的本地文件路径,类似 '/srv/www/css/bootstrap.css':
var filepath = path.join(root,pathName);
// 获取文件状态:
fs.stat(filepath,function (err,stats) {
if (!err && stats.isFile()) {
// 没有出错并且文件存在:
console.log('200 ' + request.url);
// 发送200响应:
response.writeHead(200);
// 将文件流导向response:
fs.createReadStream(filepath).pipe(responsE);
} else {
// 出错了或者文件不存在:
console.log('404 ' + request.url);
// 发送404响应:
response.writeHead(404);
response.end('404 Not Found');
}
});
});
server.listen(8080);
console.log('Server is running at http://127.0.0.1:8080/');
没有必要手动读取
文件内容。由于response对象本身是
一个Writable Stream,直接用pipe()
方法就实现了
自动读取
文件内容并
输出到
http响应。
在命令行运行node file_server.js /path/to/dir,把/path/to/dir改成你本地的
一个有效的目录,然后在浏览器中输入
http://localhost:8080/index.html:
http-index-page
只要当前目录下存在
文件index.html,服务器就可以把
文件内容发送给浏览器。观察控制台
输出:
200 /index.html
200 /css/uikit.min.css
200 /js/jquery.min.js
200 /fonts/fontawesome-webfont.woff2
第
一个请求是浏览器请求index.html
页面,后续请求是浏览器解析HTML后发送的其它资源请求。
练习
在浏览器输入
http://localhost:8080/时,会返回
404,原因是程序识别出
http请求的不是
文件,而是目录。请
修改file_server.js,如果遇到请求的路径是目录,则
自动在目录下依次
搜索index.html、default.html,如果找到了,就返回HTML
文件的
内容。
转自:
https://www.liaoxuefeng.com/wiki/001434446
689867b27157e896e74d51a89c25cc8b43bdb3000/00143
45015296018cac40c198b543fead5c549865b9bd4a000
--- end ---