• nodejs http静态服务器


    使用nodejs写的很简单的静态服务器,没有做cluster处理,没有做缓存处理,不支持访问文件夹,功能只有一个,就是获取到文件后再返回文件内容。

    var fs = require('fs');
    var url = require('url');
    var http = require('http');
    var path = require('path');
    var mime = require("./mime").mime;
    var HTTP_SERVER_PORT = 3000;
    /*
    默认页 index.html
    */
    
    var appPath = process.argv[2];
    appPath = appPath.replace(///g, '\');
    
    var proxy = http.createServer(function (req, res) {
        var reqUrl = url.parse(req.url);
        if (reqUrl.pathname == "/") {
            reqUrl.pathname = "index.html";
        }
        var realPath = path.join(appPath, decodeURIComponent(reqUrl.pathname));
        fs.exists(realPath, function (exists) {
            if (exists && isFileExists(realPath)) {
                fs.readFile(realPath, "binary", function (err, file) {
                    if (!err) {
                        var contentType = mime.lookupExtension(path.extname(realPath));
                        res.writeHead(200, {
                            "Content-Type": contentType + "; charset=utf-8",
                            "Content-Length": Buffer.byteLength(file, 'binary'),
                            "Server": "NodeJs(" + process.version + ")"
                        });
                        res.write(file, "binary");
                        res.end();
                    }
                });
            } else {
                res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
                res.write(JSON.stringify({ "error": "file not found: " + realPath }));
                res.end();
            }
        });
    });
    
    proxy.on('clientError', function (exception, socket) {
        console.log("an error has occurred in client ", exception);
    })
    
    proxy.listen(HTTP_SERVER_PORT, function () {
        console.log('Http Server running on port:' + HTTP_SERVER_PORT);
    });
    
    
    function isFileExists(filePath) {
        return fs.existsSync(filePath) && fs.statSync(filePath).isFile();
    };
    

    把代码给改了一下,但是处理流媒体的时候还是有问题。

    var proxy = http.createServer(function (req, res) {
        var reqUrl = url.parse(req.url);
        if (reqUrl.pathname == "/") {
            reqUrl.pathname = "index.html";
        }
        var realPath = path.join(appPath, decodeURIComponent(reqUrl.pathname));
        fs.exists(realPath, function (exists) {
            if (exists && isFileExists(realPath)) {
                var contentType = mime.lookupExtension(path.extname(realPath));
                res.writeHead(200, {
                    "Content-Type": contentType + "; charset=utf-8",
                    "Content-Length": fs.statSync(realPath).size,
                    "Server": "NodeJs(" + process.version + ")"
                });
                console.log(realPath);
                var fileStream = fs.createReadStream(realPath);
                fileStream.pipe(res, { end: false });
                fileStream.on('end', function () {
                    res.end();
                })
            } else {
                res.writeHead(200, { "Content-Type": "application/json; charset=utf-8" });
                res.write(JSON.stringify({ "error": "file not found: " + realPath }));
                res.end();
            }
        });
    });
    

    nodejs-http-server-2015年1月14日-104102.rar

  • 相关阅读:
    Java内存管理以及各个内存区域详解
    python数据的存储和持久化操作
    Redis的安装及配置
    POI使用详解
    遍历Map的几种方法
    Quartz的cronTrigger表达式
    Java对XML文档的增删改查
    Solr系列二:Solr与mmseg4j的整合
    cms STW 的两个阶段
    GROUP BY 和 ORDER BY 同时使用问题
  • 原文地址:https://www.cnblogs.com/grj1046/p/4223370.html
Copyright © 2020-2023  润新知