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>

 

转载于:https://www.cnblogs.com/eturn/p/4435302.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值