码迷,mamicode.com
首页 > 数据库 > 详细

Nodejs : express + sqlite + handlebars

时间:2015-04-17 17:41:15      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:

使用:

var express = require("express");
var app = express();

var list = require("./list.js");
app.get("/list/:db",list.handleHttp);
var port = 88;
console.log(port);
app.listen(port);

 

 

list.js:

var sqlite3 = require(‘sqlite3‘).verbose();
var db = new sqlite3.Database(‘jd.db‘);
var handlebars = require(‘handlebars‘);
var fs = require("fs");

function iniDB() {
    db.serialize(function() {
        var sql = "CREATE TABLE IF NOT EXISTS JDBit "
        sql += "(key TEXT, startDate timestamp, endDate timestamp, type TEXT, source TEXT, name TEXT, recordDate timestamp,";
        sql += " times INTEGER, endPrice INTEGER, status TEXT, buyer TEXT, PRIMARY KEY(key ASC))";
        db.run(sql);

        var sql2 = "insert into JDBit (key, name) select ‘1234‘,‘ABCD‘ ";
        sql2 += "union all select ‘2‘,‘B‘";
        sql2 += "union all select ‘3‘,‘C‘";
        sql2 += "union all select ‘4‘,‘D‘";
        sql2 += "union all select ‘5‘,‘E‘";
        sql2 += "union all select ‘6‘,‘F‘";
        sql2 += "union all select ‘7‘,‘G‘";
        db.run(sql2);
    });
}

function getJDData(res) {
    db.all("select * from JDBit", function(err, data) {
        var html = fs.readFileSync("web/list.htm", {
            encoding: "utf-8"
        });
        var tmpl = handlebars.compile(html);
        var rs = tmpl(data);
        res.end(rs);
    });
}

module.exports = {
    iniDB: iniDB,
    handleHttp: function(req, res, next) {
        if (req.params.db == "jd") {
            getJDData(res);
        }
    }
}

 

 

web/list.htm:

<html>
    <head></head>
    <body>
        {{#each this}}
        <a href="/{{key}}" target=‘_blank‘>{{name}}</a><br/>
        {{/each}}
    </body>
</html>

 

Nodejs : express + sqlite + handlebars

标签:

原文地址:http://www.cnblogs.com/eturn/p/4435302.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!