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. 【转】ETL增量抽取——通过时间戳方式实现

    这个实验主要思想是在创建数据库表的时候, 通过增加一个额外的字段,也就是时间戳字段, 例如在同步表 tt1 和表 tt2 的时候, 通过检查那个表是最新更新的,那个表就作为新表,而另外的表最为旧表被新 ...

  2. gvim的菜单乱码解决方法

    gvim的菜单乱码解决方法: (乱码是由于系统内码不兼容导致,系统内码包括gb2312 gb18030 utf-8 utf-16[unicode]等) 生成文件 ~/.gvimrc 并添加如下语句:s ...

  3. php.ini

    [PHP];;;;;;;;;;;;;;;;;;;; About php.ini   ;;;;;;;;;;;;;;;;;;;;; PHP's initialization file, generally ...

  4. JAVA插入sql代码

    插入数据 import java.sql.*; /** * @version 2012-02-22 * @author */ public class InsertDemo { public stat ...

  5. spring为什么不能注入static变量

    Spring 依赖注入 是依赖 set方法 set方法是 是普通的对象方法 static变量是类的属性 @Autowired private static JdbcTemplate jdbcTempl ...

  6. C++浅析——返回对象的函数

    一.原码分析 1.1 测试代码 为了方便查看拷贝构造函数调用过程,自定义了拷贝构造函数,但啥也没干. class CTEST { public: int m_nData; //Method: publ ...

  7. HDU 4578 Transformation --线段树,好题

    题意: 给一个序列,初始全为0,然后有4种操作: 1. 给区间[L,R]所有值+c 2.给区间[L,R]所有值乘c 3.设置区间[L,R]所有值为c 4.查询[L,R]的p次方和(1<=p< ...

  8. FLEX的动画

    1.使用自带效果 在Flex里面不像在Flash里面随意制作动画了,Flex更趋向于应用程序,而不是动画制作了,所以没有了时间轴的概念.在Flex中使用动画效果,可以用Flex自带的Effect,或者 ...

  9. SQL变量、运算符、分支、循环语句

    变量: SQL语言也跟其他编程语言一样,拥有变量.分支.循环等控制语句. 在SQL语言里面把变量分为局部变量和全局变量,全局变量又称系统变量. 局部变量: 使用declare关键字给变量声明,语法非常 ...

  10. android整体架构概述--①

    android的logo 是由设计师去厕所时来的灵感. 其中android的命名都是以甜点的名字来定的. android的系统一共有四层. 1.Linux内核和驱动层 2.函数库  由C或C++编写 ...