zoukankan      html  css  js  c++  java
  • Node js WEB模块

    /*
    Node.js Web 模块
    这里模拟类似PHP curl方式访问
    先创建index.html文件
    */
    //===============================以下代码为index.html文件中内容==========================================
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>测试内容</title>
    </head>
    <body>
        <h1>我的第一个标题</h1>
        <p>我的第一个段落。</p>
    </body>
    </html>
    //===============================以上代码为index.html文件中内容==========================================
    
    
    
    
    
    
    
    
    //创建服务器端
    //===============================以下代码为server.js文件中内容==========================================
    var 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 : " + pathname);
    
        // pathname.substr(1)从pathname字符串的第二个字符开始截取到结尾,这里pathname值为/index.html,pathname.substr(1)值为index.html
        // 从文件系统中读取请求的文件内容
        fs.readFile(pathname.substr(1), function (err, data) {
            if (err) {
                console.log(err);
                // HTTP 状态码: 404 : NOT FOUND
                // Content Type: text/plain
                response.writeHead(404, {'Content-Type': 'text/html'});
            }else{
                // HTTP 状态码: 200 : OK
                // Content Type: text/plain
                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/');
    //===============================以上代码为server.js文件中内容==========================================
    /*
    执行server.js
    node server.js
    输出内容:
    Server running at http://127.0.0.1:8080/
    */
    
    
    
    
    
    
    
    
    
    //创建客户端
    //===============================以下代码为client.js文件中内容==========================================
    var 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 request = http.request(options, callback);
    request.end();
    //===============================以上代码为client.js文件中内容==========================================
    /*
    新开一个终端,执行 client.js 文件
    执行client.js
    node client.js
    输出内容:
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>测试内容</title>
    </head>
    <body>
        <h1>我的第一个标题</h1>
        <p>我的第一个段落。</p>
    </body>
    </html>
    */
  • 相关阅读:
    基础网络技术--学习网络的的道路漫长啊
    华为nova8se和vivoS7e的区别哪个好
    Java.awt实现一个简单的围棋
    HashMap put原理详解(基于jdk1.8)
    Paper Pal:一个中英文论文及其代码大数据搜索平台
    【u116】最短路计数
    【u108】取数游戏
    【u106】3D模型
    【topcoder SRM 652 DIV2 250】ValueOfString
    【u103】绘制二叉树
  • 原文地址:https://www.cnblogs.com/dreamhome/p/8635646.html
Copyright © 2011-2022 走看看