Basic SQLite functionality; writing JSON into DB table
This commit is contained in:
parent
74414c1ab9
commit
c5a3c152db
@ -1,2 +1,4 @@
|
||||
FROM node:0.12.4-onbuild
|
||||
EXPOSE 3000
|
||||
RUN mkdir /usr/src/app/db
|
||||
VOLUME ["/usr/src/app/db"]
|
||||
|
@ -3,7 +3,8 @@
|
||||
"version": "0.0.1",
|
||||
"dependencies": {
|
||||
"body-parser": "^1.12.4",
|
||||
"express": "^4.12.4"
|
||||
"express": "^4.12.4",
|
||||
"sqlite3": "^3.0.8"
|
||||
},
|
||||
"scripts": { "start": "node server.js" }
|
||||
}
|
||||
|
24
server.js
24
server.js
@ -2,7 +2,25 @@ var express = require("express");
|
||||
var fs = require("fs");
|
||||
var bodyParser = require("body-parser");
|
||||
var app = express();
|
||||
var logfile = fs.createWriteStream('./log.log', {flags: 'a'});
|
||||
var logfile = fs.createWriteStream('./db/log.log', {flags: 'a'});
|
||||
|
||||
|
||||
var file = "./db/test.db";
|
||||
var exists = fs.existsSync(file);
|
||||
|
||||
if(!exists) {
|
||||
console.log("Creating DB file.");
|
||||
fs.openSync(file, "w");
|
||||
}
|
||||
|
||||
var sqlite3 = require("sqlite3").verbose();
|
||||
var db = new sqlite3.Database(file);
|
||||
|
||||
db.serialize(function() {
|
||||
if(!exists) {
|
||||
db.run("CREATE TABLE Alerts (OrigJSON TEXT)");
|
||||
}
|
||||
});
|
||||
|
||||
app.use(bodyParser.json());
|
||||
|
||||
@ -17,6 +35,10 @@ app.post('/', function(req, res){
|
||||
var source = req.body.source;
|
||||
console.log("Post event = "+postEvent+", source is "+source);
|
||||
console.log(req.body);
|
||||
|
||||
var stmt = db.prepare("INSERT INTO Alerts (OrigJSON) VALUES (?)");
|
||||
stmt.run(JSON.stringify(req.body, null, 4));
|
||||
stmt.finalize();
|
||||
//res.send(JSON.stringify(req.body, null, 4));
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user