[Node.js基础]学习⑧--http

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('

Hello world!

'); }); // 让服务器监听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,即可看到服务器响应的内容:

[Node.js基础]学习⑧--http_第1张图片
Paste_Image.png
GET: /
GET: /favicon.ico

文件服务器

资源下载地址
https://github.com/michaelliao/learn-javascript

[Node.js基础]学习⑧--http_第2张图片
Paste_Image.png

将一个字符串解析为一个Url对象:

'use strict';

var url = require('url');

console.log(url.parse('http://user:[email protected]: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:[email protected]:8080/path/to/file?query=string#hash' }

完整代码

urls.js

'use strict';

var url = require('url');

// parse url:
console.log(url.parse('http://user:[email protected]: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
[Node.js基础]学习⑧--http_第3张图片
Paste_Image.png

完整代码

js目录

index.html




    
    Index
    
    
    



    

    

Welcome

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');
});
});

你可能感兴趣的:([Node.js基础]学习⑧--http)