About
RSS

Bit Focus


NodeJS liteview Postgres Stekinscript 搭建留言板 [零]

Posted at 2012-11-19 12:31:06 | Updated at 2024-05-01 22:11:40

前期准备

配置软件与库

    需要安装
    Ubuntu 用户安装 nodejs 0.8 版本请加入下面的源
add-apt-repository ppa:chris-lea/node.js
    创建一个工作目录, 通过 npm 安装要用到的模块
npm install liteview
npm install validator

Stekinscript 快速入门 (非必需)

    请参见 https://github.com/neuront/stekinscript/wiki/Language-Specification.
    对 Stekinscript 没有兴趣的同学可以无视这一步, 在每节之后生成的 Javascript 代码会奉上, 对于 Stekinscript 代码, 可以认为它是更易阅读的 JS.

开工

上手

    创建如下内容的 main.stkn 文件, 先启动一个简单的服务器
http: require('http')

port: 8888

http.createServer((request, response):
    response.writeHead(200, { 'Content-Type':: 'text/html' })
    response.end('<html><body>Hello, World!')
).listen(port, '127.0.0.1')

console.log('Server running at local host port=' + port)
    使用 stekin 编译它 (请用合适的路径替换下面的 stekin, 或者将编译生成的 stekin 可执行程序加入 PATH)
stekin -i require < main.stkn > main.js
    生成的 JS 代码类似 (下面这是经过去除额外括号与重新排版的, 实际上现在 Stekinscript 生成的代码真是惨不忍睹)
(function () {
    const s_http = require("http");
    s_http.createServer(function (s_request, s_response) {
        s_response.writeHead(200, {
            "Content-Type": "text/html"
        });
        s_response.end("<html><body>Hello, World!");
    }).listen(8888, "127.0.0.1");
    console.log("Server running at local host port=8888");
})();
    然后使用 node 来运行它, 就在本地搭建了一枚服务器
node main.js
Server running at local host port=8888
    访问 http://localhost:8888/ 就可以看到服务器响应了.

Makefile

    在目录下加入如下内容的 Makefile, 以便更便捷地生成 JS 文件 (其中的四点 .... 表示一个制表符, Makefile 必须用这货来缩进!)
all:main.js

%.js:%.stkn
....stekin -i require -i exports < $< > $@

clean:
....rm -f *.js
    以后有其它模块加入时, 往 all 后面丢就好了.

请求路由

    现在的程序对任何请求都返回这串 "Hello, World!". 下面构建一个简单的请求路由机制, 根据请求路径派发请求.
http: require('http')
url: require('url')
port: 8888

index: (request, response):
    response.writeHead(200, { 'Content-Type':: 'text/html' })
    response.end('<html><body>Hello, World!')

app: {
    '/':: index,
}

http.createServer((request, response):
    path: url.parse(request.url).pathname
    console.log('Request to ' + path)

    if app[path]
        app[path](request, response)
    else
        response.writeHead(404, { 'Content-Type':: 'text/html' })
        response.end('<html><body>Not found')
).listen(port, '127.0.0.1')

console.log('Server running at local host port=' + port)
    重新构建一下, 然后运行服务器. 现在, 服务器对所有指向根 http://localhost:8888/ 的请求能返回 "Hello, World!" 而其它任何路径都会返回 404 错误.
    以上代码生成的 JS 如下
(function () {
    const s_http = require("http");
    const s_url = require("url");
    const s_index = function (s_request, s_response) {
        s_response.writeHead(200, {
            "Content-Type": "text/html"
        });
        s_response.end("<html><body>Hello, World!");
    };
    const s_app = {
        "/": s_index
    };
    s_http.createServer(function (s_request, s_response) {
        const s_path = s_url.parse(s_request.url).pathname;
        console.log("Request to " + s_path);
        if (s_app[s_path]) {
            s_app[s_path](s_request, s_response);
        } else {
            s_response.writeHead(404, {
                "Content-Type": "text/html"
            });
            s_response.end("<html><body>Not found");
        }
    }).listen(8888, "127.0.0.1");
    console.log("Server running at local host port=8888");
})();

使用模板

    这里采用的是 liteview 这种轻便的模板框架.
    新建 index.html 文件, 内容为
<html>
<head>
<title>Server Sample</title>
</head>
<body>
Hello, #{who}!
    然后改改 main.stkn 开头的部分
http: require('http')
url: require('url')
view: require('liteview').create()
port: 8888

index: (request, response):
    response.writeHead(200, { 'Content-Type':: 'text/html' })
    response.end(view.render('index.html', {who: 'World'}))
    这一段生成的代码类似如下
(function () {
    const s_http = require("http");
    const s_url = require("url");
    const s_view = require("liteview");
    const s_index = function (s_request, s_response) {
        s_response.writeHead(200, {
            "Content-Type": "text/html"
        });
        s_response.end(s_view.render("index.html", {"who": "World"}));
    };
    const s_app = {
        "/": s_index
    };
    s_http.createServer(function (s_request, s_response) {
        const s_path = s_url.parse(s_request.url).pathname;
        console.log("Request to " + s_path);
        if (s_app[s_path]) {
            s_app[s_path](s_request, s_response);
        } else {
            s_response.writeHead(404, {
                "Content-Type": "text/html"
            });
            s_response.end("<html><body>Not found");
        }
    }).listen(8888, "127.0.0.1");
    console.log("Server running at local host port=8888");
})();
    重新构建, 重启服务器, 然后访问它吧.

区分 GET 与 POST

    根据 GET / POST 方式区分对待请求是必要的. 下面的修改会让打开方式不对的请求获得 405 错误码.
index: {
    get: (request, response):
        response.writeHead(200, { 'Content-Type':: 'text/html' })
        response.end(view.render('index.html', {who: 'World'}))
}

app: {
    '/':: index,
}

http.createServer((request, response):
    path: url.parse(request.url).pathname
    method: request.method.toLowerCase()
    console.log('Request to ' + path + ' method=' + method)

    if app[path]
        if app[path][method]
            app[path][method](request, response)
        else
            response.writeHead(405, { 'Content-Type':: 'text/html' })
            response.end('<html><body>Method not allowed')
    else
        response.writeHead(404, { 'Content-Type':: 'text/html' })
        response.end('<html><body>Not found')
).listen(port, '127.0.0.1')
    通过 curl 测试一下 POST 请求
curl -d '' localhost:8888
<html><body>Method not allowed
    而 GET 仍然像往常一样.

    如果继续这么一个文件写下去会变得过于复杂, 下面就把这些功能拆分为两个不同的文件.
    下面是 main.stkn, 其中通过 require('./index') 导入同一路径下的 index.js 文件.
http: require('http')
url: require('url')
port: 8888

app: {
    '/':: require('./index'),
}

http.createServer((request, response):
    path: url.parse(request.url).pathname
    method: request.method.toLowerCase()
    console.log('Request to ' + path + ' method=' + method)

    if app[path]
        if app[path][method]
            app[path][method](request, response)
        else
            response.writeHead(405, { 'Content-Type':: 'text/html' })
            response.end('<html><body>Method not allowed')
    else
        response.writeHead(404, { 'Content-Type':: 'text/html' })
        response.end('<html><body>Not found')
).listen(port, '127.0.0.1')

console.log('Server running at local host port=' + port)
    还有 index.stkn, 通过 exports 导出 get 函数, 以响应 GET 方式请求.
view: require('liteview').create()

exports.get: (request, response):
    response.writeHead(200, { 'Content-Type':: 'text/html' })
    response.end(view.render('index.html', {who: 'World'}))
    最后别忘了把 index.js 加入 Makefile 需求列表中
all:main.js index.js
    现在, make & run 吧!

    附生成的 Javascript 等价代码: main.js
(function () {
    const s_http = require("http");
    const s_url = require("url");
    const s_app = {
        "/": require("./index")
    };
    s_http.createServer(function (s_request, s_response) {
        const s_path = s_url.parse(s_request.url).pathname;
        const s_method = s_request.method.toLowerCase();
        console.log("Request to " + s_path + " method=" + s_method);
        if (s_app[s_path]) {
            if (s_app[s_path][s_method]) {
                s_app[s_path][s_method](s_request, s_response);
            } else {
                s_response.writeHead(405, {
                    "Content-Type": "text/html"
                });
                s_response.end("<html><body>Method not allowed");
            }
        } else {
            s_response.writeHead(404, {
                "Content-Type": "text/html"
            });
            s_response.end("<html><body>Not found");
        }
    }).listen(8888, "127.0.0.1");
    console.log("Server running at local host port=8888");
}
})();
    index.js
(function () {
    const s_view = require("liteview");
    exports.get = function (s_request, s_response) {
        s_response.writeHead(200, {
            "Content-Type": "text/html"
        });
        s_response.end(s_view.render("index.html", {
            "who": "World"
        }));
    };
})();
    现在服务器接受响应这边架子算是搭起来了, 接下来准备加入数据库后端, 点此查看.

Post tags:   NodeJS  Stekin  Web Server  Tutorial

Leave a comment:




Creative Commons License Your comment will be licensed under
CC-NC-ND 3.0


. Back to Bit Focus
NijiPress - Copyright (C) Neuron Teckid @ Bit Focus
About this site