From c5a3c152db8065aea76f7534a151d7343f9b288e Mon Sep 17 00:00:00 2001 From: jkaplon Date: Sun, 21 Jun 2015 14:04:07 -0400 Subject: [PATCH] Basic SQLite functionality; writing JSON into DB table --- Dockerfile | 2 ++ package.json | 3 ++- server.js | 24 +++++++++++++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 13f6eb5..3670024 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,2 +1,4 @@ FROM node:0.12.4-onbuild EXPOSE 3000 +RUN mkdir /usr/src/app/db +VOLUME ["/usr/src/app/db"] diff --git a/package.json b/package.json index 140c61d..d9f475b 100644 --- a/package.json +++ b/package.json @@ -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" } } diff --git a/server.js b/server.js index 8b54ff1..34523cf 100644 --- a/server.js +++ b/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)); });