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

原文地址:https://www.cnblogs.com/grj1046/p/4223370.html