alertmon/server.js

59 lines
1.5 KiB
JavaScript

var express = require("express");
var fs = require("fs");
var bodyParser = require("body-parser");
var app = express();
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," +
"coreId TEXT," +
"locationDesc TEXT," +
"status TEXT," +
"published_at TEXT)"
);
}
});
app.use(bodyParser.json());
app.get('/', function(req, res){
//res.sendFile("/usr/src/app/index.html");
fs.createReadStream('./log.log').pipe(res);
console.log("got a GET request...");
});
app.post('/', function(req, res){
var postEvent = req.body.postEvent;
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, coreid, published_at) VALUES (?, ?, ?)");
stmt.run(
JSON.stringify(req.body, null, 4),
JSON.stringify(req.body.coreid, null, 4),
JSON.stringify(req.body.published_at, null, 4)
);
stmt.finalize();
//res.send(JSON.stringify(req.body, null, 4));
});
app.listen(3000, function() {
console.log("Started on PORT 3000");
})