node.js 初探静态服务器配置

 玩下,nodejs,对js的认识更深了。

/**
 * Created with JetBrains WebStorm.
 * User: duph
 * Date: 12-12-25
 * Time: 下午4:02
 * To change this template use File | Settings | File Templates.
 */
var http = require("http")
/*http核心模块*/
var fs = require("fs")
/*fs核心模块*/
var url = require("url")
/*url核心模块*/
var path = require("path")
var start = {
    MIME_types:{
        ".css":"text/css",
        "gif":"image/gif",
        "html":"text/html",
        "ico":"image/x-icon",
        "jpeg":"image/jpeg",
        "jpg":"image/jpeg",
        "js":"text/javascript",
        "json":"application/json",
        "pdf":"application/pdf",
        "png":"image/png",
        "svg":"image/svg+xml",
        "swf":"application/x-shockwave-flash",
        "tiff":"image/tiff",
        "txt":"text/plain",
        "wav":"audio/x-wav",
        "wma":"audio/x-ms-wma",
        "wmv":"video/x-ms-wmv",
        "xml":"text/xml"
    }, /*提前定义好所有的http类型*/
    onrequest1:function (requrest, response) {
        console.log("Server has running.");
        response.writeHead(200, {"content-typt":"text/plain"})
        response.write("<div>hello word</div>")
        response.end()
    }, /*经典的hello word,与js前端脚本的鼠标对象e类似,核心模块http的createServer方法中作为参数的回调函数,
     默认有两个参数,分别是请求对象req与响应对象res.*/
    onrequest2:function (request, response, $obj)   /*传入$obj,当前原型链的this对象*/ {
        var path_url = request.url;
        var pathname = url.parse(request.url).pathname;
        var ext = pathname.match(/(\.[^.]+|)$/)[0];//取得后缀名
        console.log(request.url)

        switch (ext) {

            case ".png":
                fs.readFile("./" + request.url, "binary", function (err, data) {//读取内容
                    if (err) throw err;
                    response.writeHead(200, {
                        "Content-Type":$obj.MIME_types[ext]
                    });

                    response.end(data, "binary");
                });
                break;
            case ".css":
                fs.readFile("./" + request.url, 'utf-8', function (err, data) {//读取内容
                    if (err) throw err;
                    response.writeHead(200, {
                        "Content-Type":$obj.MIME_types[ext]
                    });

                    response.end(data, "utf-8");
                });
                break;
            case ".js":
            case ".html":
                fs.readFile("./" + request.url, 'utf-8', function (err, data) {//读取内容
                    if (err) throw err;
                    response.writeHead(200, {
                        "Content-Type":$obj.MIME_types[ext]
                    });

                    response.end(data, "utf-8");
                });
                break;
            default:

                fs.readdir("./"+ request.url, function (err, filenames) {
                    var i;
                    for (i = 0; i < filenames.length; i++) {
                        console.log("filename: "+filenames[i]);
                    }
                })/*输出目录下文件*/;

                fs.readFile('./'+request.url+"/index.html", 'utf-8', function (err, data) {//读取内容
                    if (err) throw err;
                    response.writeHead(200, {
                        "Content-Type":"text/html"
                    })/*默认输出index.html*/;

                    response.end(data,"utf-8");
                });

        }

    }, /*页面输出*/

    server_start:function () {
        var $this = this
        http.createServer(function (request, response) {

            $this.onrequest2(request, response, $this)
        }).listen(8881)

        console.log(path.resolve(__filename))
        console.log("server start")
    }
}
exports.start = start

 

 新建index.js

/**
 * Created with JetBrains WebStorm.
 * User: duph
 * Date: 12-12-24
 * Time: 下午5:49
 * To change this template use File | Settings | File Templates.
 */
var server=require ("./mm/ss/server2")/*没有打包直接require的文件*/
server.start.server_start()/*启动服务器*/

 

https://github.com/breakfriday/nodejs

 

posted @ 2012-12-27 14:40  break_happy  Views(445)  Comments(0Edit  收藏  举报