Files
seasonedShows/server.js
2017-04-07 12:48:30 +02:00

86 lines
2.6 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// server.js
// BASE SETUP
// =============================================================================
// call the packages we need
var express = require('express'); // call express
var app = express(); // define our app using express
var bodyParser = require('body-parser');
var sqlite3 = require('sqlite3').verbose();
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var port = 31459; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = express.Router(); // get an instance of the express Router
// test route to make sure everything is working (accessed at GET http://localhost:8080/api)
router.get('/', function(req, res) {
res.json({ message: 'hooray! welcome to our api!' });
});
router.get('/seasoned', function(req, res) {
var db_path = 'shows.db';
var db = new sqlite3.Database(db_path);
var returnMsg;
var id = req.param('id');
function getEpisode(id, fn){
db.serialize(function() {
db.get("SELECT * FROM stray_eps WHERE id = '" + id + "'", function(err, row) {
fn(row)
})
});
db.close();
}
getEpisode(id, function(episode){
res.setHeader('Access-Control-Allow-Origin', 'https://kevinmidboe.com');
res.json({episode}); // this is where you get the return value
});
});
router.post('/seasoned', function (req, res) {
console.log(req.body['id']);
var db_path = 'shows.db';
var db = new sqlite3.Database(db_path);
var UPDATE_DATA = "UPDATE stray_eps SET verified=$verified WHERE id=$id";
db.serialize(function() {
// var stmt = db.prepare("UPDATE stray_eps SET verified=? WHERE id=?");
// for (key in req.body) {
// stmt.run(req.body[key]);
// console.log(req.body[key]);
// }
db.run(UPDATE_DATA, {$verified: req.body['verified'], $id: req.body['id']}, function(err) {
if (err) {
console.log(err);
} else {
console.log('UPDATE DATA');
}
});
// stmt.run([req.body['verified'], req.body['id']]);
// stmt.finalize();
db.close();
});
res.setHeader('Access-Control-Allow-Origin', 'https://kevinmidboe.com');
res.json({message: 'updated'});
})
// more routes for our API will happen here
// REGISTER OUR ROUTES -------------------------------
// all of our routes will be prefixed with /api
app.use('/api', router);
// START THE SERVER
// =============================================================================
app.listen(port);
console.log('Magic happens on port ' + port);