zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    Django之模板系统
    Django之ORM操作
    Django之路由分配系统
    基于模态对话框 学生管理系统
    初识Django
    ORM框架之SQLALchemy
    MySQL查询性能调优化
    MySQL练习题
    MySQL自带功能介绍
    javascript时间戳和日期字符串相互转换
  • 原文地址:https://www.cnblogs.com/grj1046/p/4223370.html
Copyright © 2011-2022 走看看