標籤:
使用:
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