node.js实现web服务器还是比较简单的,我了解node.js是从《node入门》开始的,如果你不了解node.js也可以看看!

我根据那书一步一步的练习完了,也的确大概了解了node.js,不过里面写的路由的地方总感觉不方便,十一放假最后一天,试着写了个简单的web服务器,现在分享记录于此!

http模块已提供了基本功能,所以我主要解决两个问题,1是静态资源的处理,2是动态资源的路由。

静态资源在node.js里的意思是不变的,如图片、前端js、css、html页面等。

动态资源我们一般指aspx页面,ashx页面,asp页面,jsp页面,php页面等,而node.js里其实没动态资源这一说,它对请求的处理都是由回调方法完成的,在我实现的httserver里,借鉴了ashx的写法,把处理请求的js文件看作动态资源。

首先实现一个处理静态资源的函数,其实就是对本地文件的读取操作,这个方法已满足了上面说的静态资源的处理。

//处理静态资源
function staticResHandler(localPath, ext, response) {
    fs.readFile(localPath, "binary", function (error, file) {
        if (error) {
            response.writeHead(500, { "Content-Type": "text/plain" });
            response.end("Server Error:" + error);
        } else {
            response.writeHead(200, { "Content-Type": getContentTypeByExt(ext) });
            response.end(file, "binary");
        }
    });
}

而动态资源肯定不能一个方法搞定,就像你的网站有register.aspx、login.aspx等等,都需要你自己来写,在我的httpserver里,每个处理请求的js模块都导出processRequest(request,response)即可,比如实现一个register.js(只输出字符串register)

exports.processRequest = function (request, response) {
    response.writeHead(200, { 'Content-Type': 'text/plain' });
    resp.end("register");
}

现在当请求到来时,我们要做的就是决定怎么处理,即路由。

因为静态资源url指定静态资源大家都很习惯了,所以这里不变,比如

访问http://localhost/img/logo.png  就是访问 web根目录\img\logo.png;

访问http://localhost/js/what.js 就是访问 web根目录\js\what.js;

而动态资源也是一般的js文件,即服务器端js,就比如我实现的这个httpserver.js和上面说的register.js都是不应该让用户访问的,所以路由的时候要判断,就是一些if、else,简单而强大是我的最爱,这里只看最后的的判断,

    fs.exists(localPath, function (exists) {
        if (exists) {
            if (staticRes) {
                staticResHandler(localPath, ext, response); //静态资源
            } else {
                try {
                    var handler = require(localPath);
                    if (handler.processRequest && typeof handler.processRequest === 'function') {
                        handler.processRequest(request, response); //动态资源
                    } else {
                        response.writeHead(404, { 'Content-Type': 'text/plain' });
                        response.end('404:Handle Not found');
                    }
                } catch (exception) {
                    console.log('error::url:' + request.url + 'msg:' + exception);
                    response.writeHead(500, { "Content-Type": "text/plain" });
                    response.end("Server Error:" + exception);
                }
            }
        } else { //资源不存在
            response.writeHead(404, { 'Content-Type': 'text/plain' });
            response.end('404:File Not found');
        }
    });

处理静态资源上面已说过了,请看处理动态资源的那两句,localPath是相对web根目录的后端js的路径,如果上面register.js在 根目录/src/account文件夹里,那么你的url请求就是http://localhost/account/register,而这时localPath就是./src/account/register.js,注意这里不是MVC,只是url没有src路径和.js后缀而已,那么为什么要这样呢?就是为了和前端js文件区分开!

再有,没有配置的程序不是好程序,不过我的配置总是很烂的配置!(你可能感觉我写的很乱,不过没关系,后面给出完整代码,看一下就清楚了,如果你感觉不错,下载了事例在你电脑上运行了,那我也倍感荣幸了!)

//配置
var config = {
    port: 80,
    denyAccess: ['./httpserver.js', './src/requirecache.js'],
    localIPs: ['127.0.0.1'],
    srcpath: '/src'
};
./src/requirecache.js这个文件是干什么的呢?这里要说明一下,require这个方法是有缓存机制的,它把加载过的模块都缓存到require.cache这个对象中,当第二次require的时候就直接回返缓存的模块了,当然这样是为性能考虑,但是我修改一下register.js是不想重启web服务器的,如果你感觉无所谓,那这个特殊的动态资源就不需要了,请明白,requirecache.js和register.js是被一样看待的,都是处理请求的js文件。requirecache.js模块的功能就是删除模板缓存:
var querystring=require('querystring');
var url=require('url');

exports.processRequest = function (request, response) {
    response.writeHead(200, { 'Content-Type': 'text/html' });
    var qs= querystring.parse(url.parse(request.url).query);
    if(qs.key){
        delete require.cache[qs.key];
    }
    response.write('<html><head></head><body>');
    for(var key in require.cache){
        response.write('<a href="?key='+key+'">'+key+'</a><br/>');
    }
    response.write('<a href="?">View</a><br/>');
    response.end('</body></html>');
}

运行了node httpserver.js后,打开http://localhost/requirecache大概是这样:

然后就是删哪个点哪个就可以(对于httpserver.js来说,是一个很实用小功能)。

源代码有两个文件:

httpserver.js(这个是必须的)

var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');

//配置
var config = {
    port: 80,
    denyAccess: ['./httpserver.js', './src/requirecache.js'],
    localIPs: ['127.0.0.1'],
    srcpath: '/src'
};

//开始HTTP服务器
http.createServer(processRequestRoute).listen(config.port);
console.log("Server has started. port:"+config.port);

//路由URL
function processRequestRoute(request, response) {
    var pathname = url.parse(request.url).pathname;
    if (pathname === '/') {
        pathname = "/index.html"; //默认页面
    }
    var ext = path.extname(pathname);
    var localPath = ''; //本地相对路径
    var staticres = false; //是否是静态资源
    if (ext.length > 0) {
        localPath = '.' + pathname;
        staticRes = true;
    } else {
        localPath = '.' + config.srcpath + pathname + '.js';
        staticRes = false;
    }
    //禁止远程访问
    if (config.denyAccess && config.denyAccess.length > 0) {
        var islocal = false;
        var remoteAddress = request.connection.remoteAddress;
        for (var j = 0; j < config.localIPs.length; j++) {
            if (remoteAddress === config.localIPs[j]) {
                islocal = true;
                break;
            }
        }
        if (!islocal) {
            for (var i = 0; i < config.denyAccess.length; i++) {
                if (localPath === config.denyAccess[i]) {
                    response.writeHead(403, { 'Content-Type': 'text/plain' });
                    response.end('403:Deny access to this page');
                    return;
                }
            }
        }
    }
    //禁止访问后端js
    if (staticRes && localPath.indexOf(config.srcpath) >= 0) {
        response.writeHead(403, { 'Content-Type': 'text/plain' });
        response.end('403:Deny access to this page');
        return;
    }

    fs.exists(localPath, function (exists) {
        if (exists) {
            if (staticRes) {
                staticResHandler(localPath, ext, response); //静态资源
            } else {
                try {
                    var handler = require(localPath);
                    if (handler.processRequest && typeof handler.processRequest === 'function') {
                        handler.processRequest(request, response); //动态资源
                    } else {
                        response.writeHead(404, { 'Content-Type': 'text/plain' });
                        response.end('404:Handle Not found');
                    }
                } catch (exception) {
                    console.log('error::url:' + request.url + 'msg:' + exception);
                    response.writeHead(500, { "Content-Type": "text/plain" });
                    response.end("Server Error:" + exception);
                }
            }
        } else { //资源不存在
            response.writeHead(404, { 'Content-Type': 'text/plain' });
            response.end('404:File Not found');
        }
    });
}

//处理静态资源
function staticResHandler(localPath, ext, response) {
    fs.readFile(localPath, "binary", function (error, file) {
        if (error) {
            response.writeHead(500, { "Content-Type": "text/plain" });
            response.end("Server Error:" + error);
        } else {
            response.writeHead(200, { "Content-Type": getContentTypeByExt(ext) });
            response.end(file, "binary");
        }
    });
}

//得到ContentType
function getContentTypeByExt(ext) {
    ext = ext.toLowerCase();
    if (ext === '.htm' || ext === '.html')
        return 'text/html';
    else if (ext === '.js')
        return 'application/x-javascript';
    else if (ext === '.css')
        return 'text/css';
    else if (ext === '.jpe' || ext === '.jpeg' || ext === '.jpg')
        return 'image/jpeg';
    else if (ext === '.png')
        return 'image/png';
    else if (ext === '.ico')
        return 'image/x-icon';
    else if (ext === '.zip')
        return 'application/zip';
    else if (ext === '.doc')
        return 'application/msword';
    else
        return 'text/plain';
}

httpserver.js

requirecache.js(这个是很有用的,要放到config.srcpath路径下)

var querystring=require('querystring');
var url=require('url');

exports.processRequest = function (request, response) {
    response.writeHead(200, { 'Content-Type': 'text/html' });
    var qs= querystring.parse(url.parse(request.url).query);
    if(qs.key){
        delete require.cache[qs.key];
    }
    response.write('<html><head></head><body>');
    for(var key in require.cache){
        response.write('<a href="?key=' + key + '">' + key + '</a><br/>');
    }
    response.write('<a href="?">View</a><br/>');
    response.end('</body></html>');
}

requirecache.js

完成例子下载

用node.js实现简单的web服务器的更多相关文章

  1. Fenix – 基于 Node.js 的桌面静态 Web 服务器

    Fenix 是一个提供给开发人员使用的简单的桌面静态 Web 服务器,基于 Node.js 开发.您可以同时在上面运行任意数量的项目,特别适合前端开发人员使用. 您可以通过免费的 Node.js 控制 ...

  2. iOS 使用node js 搭建简单的本地服务器

    一.前提:基于iOS 项目 调用,使用了第三方框架NodeMobile.技术说明关键是 应用生命整个周期只能在应用启动时候开辟的一个线程里申请 一个 node  js 资源.如果终止了运行,重启是不支 ...

  3. Nodejs入门-基于Node.js的简单应用

    服务端JavaScript 众所周知的,JavaScript是运行在浏览器的脚本语言,JavaScript通常作为客户端程序设计语言使用,以JavaScript写出的程序常在用户的浏览器上运行.直至N ...

  4. 学习用node.js建立一个简单的web服务器

    一.建立简单的Web服务器涉及到Node.js的一些基本知识点: 1.请求模块 在Node.js中,系统提供了许多有用的模块(当然你也可以用JavaScript编写自己的模块,以后的章节我们将详细讲解 ...

  5. 使用 Nodejs 搭建简单的Web服务器

    使用Nodejs搭建Web服务器是学习Node.js比较全面的入门教程,因为要完成一个简单的Web服务器,你需要学习Nodejs中几个比较重要的模块,比如:http协议模块.文件系统.url解析模块. ...

  6. node.js之十大Web框架

    之前接触过Node.js是因为好奇大前端越来越能干了,连我后台的饭碗都要抢了,太嚣张了,于是我想打压打压它,然后就这样接触它了.再到后来是因为Settings-Sync插件二次开发,我需要用node. ...

  7. node创建一个简单的web服务

    本文将如何用node创建一个简单的web服务,过程也很简单呢~ 开始之前要先安装node.js 1.创建一个最简单的服务 // server.js const http = require('http ...

  8. 利用 nodeJS 搭建一个简单的Web服务器(转)

    下面的代码演示如何利用 nodeJS 搭建一个简单的Web服务器: 1. 文件 WebServer.js: //-------------------------------------------- ...

  9. 自己动手模拟开发一个简单的Web服务器

    开篇:每当我们将开发好的ASP.NET网站部署到IIS服务器中,在浏览器正常浏览页面时,可曾想过Web服务器是怎么工作的,其原理是什么?“纸上得来终觉浅,绝知此事要躬行”,于是我们自己模拟一个简单的W ...

随机推荐

  1. 使用ImitateLogin模拟登录百度

    在之前的文章中,我已经介绍过一个社交网站模拟登录的类库:imitate-login ,这是一个通过c#的HttpWebRequest来模拟网站登录的库,之前实现了微博网页版和微博Wap版:现在,模拟百 ...

  2. 【转】MySQL 高可用架构在业务层面的分析研究

    原文地址 http://database.51cto.com/art/201507/483463_all.htm 前言: 相对于传统行业的相对服务时间9x9x6或者9x12x5,因为互联网电子商务以及 ...

  3. 烂泥:Linux源码包制作RPM包之Apache

    本文由秀依林枫提供友情赞助,首发于烂泥行天下 公司服务器比较多,需要把apache源码包制作成rpm包,然后放到公司内网yum源上进行下载安装.apache的rpm包安装方式比源码安装方式比较快,这能 ...

  4. 项目搭建系列之一:使用Maven搭建SpringMVC项目

    约定电脑都安装了eclipse,且已配置好Maven以及eclipse插件. 1.Eclipse 2.maven 3.Eclipse 需要安装maven插件.url:maven - http://do ...

  5. x01.os.14: 时间都去哪儿了

    时间都去哪儿了 老帕的“花儿为什么这样红”,三分钟引起六次欢呼,却败给了张碧晨.试想一下,如果是人气更高的陈冰,即使唱得和张碧晨一模一样,可能仍然不敌老帕,为什么张碧晨就能取胜呢?有这么个笑话:一人弹 ...

  6. Linux dd

    一.简介 二.实例 1)复制二进制文件指定章节 iflag=skip_bytes bs= count= of=test 2)修改二进制文件指定章节 oflag=seek_bytes bs= count ...

  7. Uploadify v3.2.1 参数说明

    一.属性 属性名称 默认值 说明 auto true 设置为true当选择文件后就直接上传了,为false需要点击上传按钮才上传 . buttonClass ” 按钮样式 buttonCursor ‘ ...

  8. 时光煮雨 Unity3d 序列目标点的移动①

    系列目录 [Unity3D基础]让物体动起来①--基于UGUI的鼠标点击移动 [Unity3D基础]让物体动起来②--UGUI鼠标点击逐帧移动 时光煮雨 Unity3D让物体动起来③—UGUI DoT ...

  9. 老话题:自己编写只截窗口客户区的截屏软件(VB2010)

    现在能实现截屏的软件很多,就不一一列举了,连WIN7都自带截屏软件,甚至OFFICE2010开始都有截屏的功能. 截屏软件虽多,无外乎三种截屏方式:全屏截图.窗口截图.自定义矩形截图. 其中,窗口截图 ...

  10. Java基础-super关键字与this关键字

    用super调用父类构造方法 类如果没有显式声明构造方法,则自动生成无参数的默认构造方法. 1.先用一个无参数的父类构造方法验证一下, 执行子类构造方法会自动调用父类的构造方法.测试代码: class ...