Nodejs : express + sqlite + handlebars

使用:

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>

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。