HTTP协议
HTTP服务器
'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/
不要关闭命令提示符,直接打开浏览器输入http://localhost:8080,即可看到服务器响应的内容:
GET: /
GET: /favicon.ico
文件服务器
资源下载地址
https://github.com/michaelliao/learn-javascript
将一个字符串解析为一个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' }
完整代码
urls.js
'use strict';
var url = require('url');
// parse url:
console.log(url.parse('http://user:pass@host.com:8080/path/to/file?query=string#hash'));
// parse incomplete url:
console.log(url.parse('/static/js/jquery.js?name=Hello%20world'));
// construct a url:
console.log(url.format({
protocol: 'http',
hostname: 'localhost',
pathname: '/static/js',
query: {
name: 'Nodejs',
version: 'v 1.0'
}
}));
protocol: null,
slashes: null,
auth: null,
host: null,
port: null,
hostname: null,
hash: null,
search: '?name=Hello%20world',
query: 'name=Hello%20world',
pathname: '/static/js/jquery.js',
path: '/static/js/jquery.js?name=Hello%20world',
href: '/static/js/jquery.js?name=Hello%20world' }
http://localhost/static/js?name=Nodejs&version=v%201.0
文件服务器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/');
命令行运行
node file_server.js D:\workspace\js\static
浏览器
http://localhost:8080/static/index.html
完整代码
js目录
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Index</title>
<link rel="stylesheet" href="css/uikit.min.css" />
<script src="js/jquery.min.js"></script>
<script>
$(function () {
$('#code').click(function () {
window.open('https://github.com/michaelliao/learn-javascript/tree/master/samples/node/http');
});
});
</script>
</head>
<body>
<div id="header" class="uk-navbar uk-navbar-attached">
<div class="uk-container x-container">
<div class="uk-navbar uk-navbar-attached">
<a href="index.html" class="uk-navbar-brand"><i class="uk-icon-home"></i> Learn Nodejs</a>
<ul id="ul-navbar" class="uk-navbar-nav">
<li><a target="_blank" href="http://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000">JavaScript教程</a></li>
<li><a target="_blank" href="http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000">Python教程</a></li>
<li><a target="_blank" href="http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000">Git教程</a></li>
</ul>
</div>
</div>
</div><!-- // header -->
<div id="main">
<div class="uk-container">
<div class="uk-grid">
<div class="uk-width-1-1 uk-margin">
<h1>Welcome</h1>
<p><button id="code" class="uk-button uk-button-primary">Show me the code</button></p>
</div>
</div>
</div>
</div>
<div id="footer">
<div class="uk-container">
<hr>
<div class="uk-grid">
<div class="uk-width-1-1">
<p>Copyright©2015-2016</p>
</div>
</div>
</div>
</div>
</body>
</html>
file_server.js
'use strict';
//node file_server.js D:\workspace\js\http\static
//http://127.0.0.1:8080/index.html
var
fs = require('fs'),
url = require('url'),
path = require('path'),
http = require('http');
var root = path.resolve(process.argv[2] || '.');
console.log('process.argv[2]: ' + process.argv[2]);// D:\workspace\js\http\static
console.log('Static root dir: ' + root);//D:\workspace\js\http\static
var server = http.createServer(function (request, response) {
var
pathname = url.parse(request.url).pathname,
filepath = path.join(root, pathname);
console.log('request.url: ' + request.url);///index.html
console.log('request.pathname: ' + url.parse(request.url).pathname);// /index.html
console.log('filepath: ' + filepath);//D:\workspace\js\http\static\index.html
fs.stat(filepath, function (err, stats) {
if (!err && stats.isFile()) {
console.log('200 ' + request.url);
response.writeHead(200);
fs.createReadStream(filepath).pipe(response);
} else {
console.log('404 ' + request.url);
response.writeHead(404);
response.end('404 Not Found');
}
});
});
server.listen(8080);
console.log('Server is running at http://127.0.0.1:8080/');
练习
在浏览器输入http://localhost:8080/ 时,会返回404,原因是程序识别出HTTP请求的不是文件,而是目录。请修改file_server.js,如果遇到请求的路径是目录,则自动在目录下依次搜索index.html、default.html,如果找到了,就返回HTML文件的内容。
// 创建服务器:
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);
var promise_f=function(resolve,reject){
fs.stat(filepath, function (err, stats) {
if (!err) {
if(stats.isFile()){
resolve(filepath);
}else if(stats.isDirectory()){
console.log('isDirectory' + request.url);
var filename=path.join(root+pathname,'index.html')
console.log('search' + filename);
fs.stat(filename,function(err,stats){
if (!err&&stats.isFile()) {
resolve(filename);
}
else{
reject('404 ' + request.url);
}
})
filename=path.join(root+pathname,'default.html')
console.log('search' + filename);
fs.stat(filename,function(err,stats){
if (!err&&stats.isFile()) {
resolve(filename);
}else{
reject('404 ' + request.url);
}
})
}
}else{
// 发送404响应:
reject('404 ' + request.url);
}
});
};
// 获取文件状态:
new Promise(promise_f)
.then(function(res){
// 没有出错并且文件存在:
console.log('200 ' + request.url);
// 发送200响应:
response.writeHead(200);
// 将文件流导向response:
fs.createReadStream(filepath).pipe(response);
}).catch(function(err){
// 出错了或者文件不存在:
console.log('404 ' + request.url);
response.writeHead(404);
response.end('404 Not Found');
});
});