轻量级web框架node.js和bottle

来源:互联网 发布:《云计算安全》 编辑:程序博客网 时间:2024/04/30 08:42

一.介绍

nodejs是一个轻量级的webserver框架,和python的bottle很类似,都是轻量级的web框架:写一个web server只需要一行代码就可以。

node.js平台的构建是基于Chrome's JavaScript runtime,即它是对GoogleV8引擎(应用于Google Chrome浏览器)进行了封装。V8引擎执行Javascript的速度非常快,性能非常好。Node对一些特殊用例进行了优化,提供了替代的API,使得V8在非浏览器环境下运行得更好。

Node.js的官网为:http://www.nodejs.org/
Bottle的官网为:http://bottlepy.org/docs/dev/

二.安装说明

linux下的安装命令如下所示:

wget http://nodejs.org/dist/v0.10.5/node-v0.10.5.tar.gz
tar zxvf node-v0.10.5.tar.gz
cd node-v0.10.5.tar.gz
./configure --prefix=/home/zhaolincheung/local/nodejs
make && make intall

注:这里将node.js安装在/home/zhaolincheung/local/nodejs目录下。node.js的安装需要python2.6以上的支持,否则在执行./configure时会出错;node.js还需要gcc-c++的支持,所以系统需要实现安装gcc-c++。

通过 node -v来检查安装是否成功,如果返回:v.0.10.5,则说明安装成功。

至此node.js已经编译并安装完成。如需卸载,可以执行make uninstall进行卸载。

三.简单的hello world程序

学习任何语言或者框架,首先要写的程序就是hello world程序。这里也是这样,我们来写一个简单的hello world程序。

首先,编写helloworld.js,内容如下:

var http = require('http');http.createServer(function(req, res) {    res.writeHead(200, {'Content-Type':'text/plain'});    res.end('Hello World\n');}).listen(10001);console.log('Server running at http://127.0.0.1:10001/');
其次,执行该文件:/home/zhaolincheung/local/nodejs/bin/node  helloworld.js
最后,通过浏览器访问http://127.0.0.1:1337便得到了hello world的响应。

四.对不同的请求进行路由

当我们在使用node.js进行开发时,由于node.js是单线程的,所以对于耗时的操作,我们将它封装成函数采用回调函数的方式进行调用,这样可以让代码继续往下执行。当把耗时操作放在回调函数中,当耗时操作执行完毕后,回调函数会继续执行其函数内剩余的代码,它不会耽误回调函数外其他方法(请求)的执行。这个就是传说中的 回调 。我们给某个方法传递了一个函数,这个方法在有相应事件发生时调用这个函数来进行回调。这就是事件驱动的异步服务器端JavaScript和它的回调啦!

作者在网上看到了一个很好的node.js的入门资料,分享给大家,参考链接为:http://www.nodebeginner.org/index-zh-cn.html

下面介绍该资料中的一个例子,对不同的请求(url)进行相应的处理(路由)。它包含4个文件,index.js、server.js、router.js和requestHandlers.js。

其中index.js是定义了不同请求(url)的相应的处理(路由)方法,和启动webserver。

index.js的代码如下:

    var server = require("./server");    var router = require("./router");    var requestHandlers = require("./requestHandlers");    var handle = {};    handle["/"] = requestHandlers.start;    handle["/start"] = requestHandlers.start;    handle["/upload"] = requestHandlers.upload;    server.start(router.route, handle);

server.js是server的创建和启动的实现。server.js的代码如下:

    var http = require("http");    var url = require("url");       function start(route, handle) {        function onRequest(request, response) {            var pathname = url.parse(request.url).pathname;            console.log("Request for " + pathname + " received.");                        route(handle, pathname, response);                   }                 http.createServer(onRequest).listen(65535);        console.log('Server has started.');    }         exports.start = start;

上面的onRequest函数就是回调函数。
回调函数就是将一个函数f1作为函数f2的参数进行调用,这样f2就不会因为f1的耗时操作而影响下面代码的执行,f1会等待相应事件的触发,即f1和f2会并行执行。

router.js是对不同的url调用函数(即下面的handler[pathname](response))进行处理。router.js的代码如下:

    function route(handle, pathname, response) {        console.log("About to route a request for "+ pathname);        if (typeof handle[pathname] == 'function') {            handle[pathname](response);        } else {            console.log("No request handler found for " + pathname);            response.writeHead(404,{'Content-Type': 'text/plain'});            response.write('node.js: 404 Not found');            response.end();        }     }    exports.route = route;
requestHandlers.js是不同url调用(路由)的方法的具体实现。requestHandlers.js的代码如下:

    function start(response) {        console.log("Request handler 'start' was called.");        response.writeHead(200, {'Content-Type': 'text/plain'});        response.write("node.js: hello,start");        response.end();    }    function upload(response){        console.log("Request handler 'upload' was called.");            response.writeHead(200, {'Content-Type': 'text/plain'});        response.write("node.js: hello,upload");        response.end();    }    exports.start = start;    exports.upload = upload;

运行这个例子的命令为:node index.js

当请求:http://127.0.0.1:65535/后的运行结果如下:


当请求:http://127.0.0.1:65535/upload后的运行结果如下:


代码链接:https://github.com/zhaolincheung/nodejs_demo

五.开发进阶(加载静态html/js/css文件)

下面我们在上面例子的基础上实现:能够加载.html页面、.js文件和.css文件。这里只需要对server.js进行修改即可,server.js的内容如下:

    var http = require("http");    var url = require("url");    var fs = require('fs');    var path = require('path');    var root = '/home/zhaolianxiang1/test_nodejs'; //定义文件的根目录    function start(route, handle) {        function onRequest(request, response) {            var pathname = url.parse(request.url).pathname;            console.log("Request for " + pathname + " received.");            //获取文件的后缀名            var ext = path.extname(pathname);            switch (ext) {                case '.html':                case '.css':                case '.js':                    var realpath = root + pathname;                    //判断请求的文件是否存在                    path.exists(realpath, function (exists) {                        if (exists) {                            //存在的话,读取文件                            fs.readFile('.' + request.url, 'utf-8', function (err, data) {                                if (err) throw err;                                response.writeHead(200, {                                    "Content-Type" : {                                        ".html": "text/html",                                        ".css" : "text/css",                                        ".js"  : "application/javascript",                                    }[ext]                                });                                response.write(data);                                response.end();                            });                        } else {//请求的文件不存在                            response.writeHead(404, {"Content-Type" : "text/html"});                            response.end("<h1>404 Not Found</h1>");                        }                    });                    break;                default://其他请求进行路由                    route(handle, pathname, response);            }        }        http.createServer(onRequest).listen(65535);        console.log('Server has started.');    }    exports.start = start;

然后,我们在当前目录下再建一个文件index.html。

好,接下来我们来看下运行结果,在浏览器输入:http://127.0.0.1:65535/index.html,运行结果如下:

在浏览器输入:http://127.0.0.1:65535/start,运行结果如下:

参考链接:http://www.cnblogs.com/rubylouvre/archive/2011/11/20/2255083.html

六.python的轻量级web框架bottle介绍

编写一个文件bottle_example.py,内容如下:

from bottle import route, run, template, static_file@route('/hello')def hello():    return "Hello,world!"test_home = './resource/'@route('/rsrc/<p:path>')def foo(p):    return static_file(p, test_home)run(host='localhost', port=8080)

执行这个文件,然后在浏览器中输入:http://localhost:8080/hello,页面会输出”hello,world”。

如下所示:


好,webserver已经跑起来了。接下来,我们研究webserver如何加载一个页面,首先我们在bottle_example.py的当前目录下新建一个文件夹resource,在resource目录下再建立一个docs文件夹,在docs目录下我们建一个index.html页面(即index.html存在在./resource/docs/目录下)。目录结构如下图所示:


因为bottle是基于路由进行响应处理的,我们该如何让bottle来加载这个index.html页面呢?
答案是:@route('/rsrc/<p:path>')这个路由能够实现。当用户在浏览器中打开http://localhost:8080/rsrc/docs/index.html页面时,就会触发这个路由,然后执行static_file这个方法来加载path路径指定的静态文件。这样便实现了加载静态页面的功能。



 
原创粉丝点击