Created
November 3, 2018 15:50
-
-
Save spddl/b507494f5e1e261e4bd7c80794b6b6ab to your computer and use it in GitHub Desktop.
REST API + LowDB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict' | |
const port = process.env.PORT || 8080 | |
const express = require('express') // https://expressjs.com/de/ | |
const bodyParser = require('body-parser') // https://www.npmjs.com/package/body-parser | |
const cors = require('cors') // https://www.npmjs.com/package/cors | |
const app = express() | |
app.use(cors()) | |
app.get('/status', function (_, res) { | |
console.log('GET /status') | |
// Gibt die "Datenbank" zurück | |
res.json(db.get('data').value()) | |
}) | |
app.get('/refresh', function (_, res) { | |
console.log('GET /refresh') | |
// Falls du die db.json manuell änderst kannst du sie hier aktualisieren | |
db.read() | |
res.sendStatus(200) // OK | |
}) | |
app.use(bodyParser.urlencoded({ extended: true })) | |
app.use(bodyParser.json()) | |
app.post('/api', function (req, res) { | |
console.log('POST /api', req.body) | |
// res.json(getStatus(req.body)) // Hardgecodet | |
res.json(getDBStatus(req.body)) // Lowdb | |
}) | |
app.post('/new', function (req, res) { | |
console.log('POST /new', req.body) | |
if (req.body.result) { | |
// TODO: Evtl. noch eine Logik die prüft ob es schon ein Eintrag dazu gibt? | |
db.get('data').push(req.body).write() // wird in der db.json gespeichert | |
res.sendStatus(200) // OK | |
} else { | |
res.status(400).send('Ohne "result" kann das nicht gespeichert werden') // geht schon würde nur kein Sinn machen :P | |
} | |
}) | |
app.listen(port) | |
console.log('listen on port', port) | |
// Hier könnte man jegliche Logik schreiben nur wäre das Hardgecodet und liese sich nur durch einen Neustart aktualisieren | |
// function getStatus (json) { | |
// if (json.troubleshoot.toLowerCase() === 'komplettausfall') { | |
// if (json.ledDSL === 'true') { // auch wenn das ein Bool Wert war... er wird über den bodyParser als String interpretiert | |
// return { err: false, data: 'dann mache dies und das!' } | |
// } else { | |
// return { err: false, data: 'ich würde ja grundlegend alles anders machen' } | |
// } | |
// } | |
// return { err: 'nichts trifft zu' } | |
// } | |
// LowDB | |
const low = require('lowdb') // https://github.com/typicode/lowdb | |
const FileSync = require('lowdb/adapters/FileSync') | |
const adapter = new FileSync('db.json') | |
const db = low(adapter) | |
// Set some defaults (required if your JSON file is empty) | |
db.defaults({ data: {} }).write() | |
function getDBStatus (json) { | |
// var data = db.get('data') // so könnte das ausgeschrieben aussehen | |
// .find({ | |
// troubleshoot: 'komplettausfall', | |
// ledDSL: 'false', | |
// ledPower: 'true' | |
// }) | |
// .value() | |
var data = db.get('data') | |
.find(json) | |
.value() | |
if (data) { | |
return data // Gibt die gefundenen Daten zurück | |
} else { | |
return { data: 'nichts trifft zu' } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html lang="en"> | |
<head> | |
<!-- Required meta tags --> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> | |
<!-- Bootstrap CSS --> | |
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous"> | |
<title>Hello, world!</title> | |
</head> | |
<body> | |
<div class="container"> | |
<h1>Test umgebung :P</h1> | |
<div class="input-group mb-3"> | |
<div class="input-group-prepend"> | |
<span class="input-group-text" id="inputGroup-troubleshoot">Troubleshoot</span> | |
</div> | |
<input type="text" class="form-control" id="troubleshoot" aria-label="Sizing example input" aria-describedby="inputGroup-troubleshoot" value="Komplettausfall"> | |
</div> | |
<div class="custom-control custom-checkbox"> | |
<input type="checkbox" class="custom-control-input" id="ledDSL"> | |
<label class="custom-control-label" for="ledDSL">DSL leuchtet?</label> | |
</div> | |
<div class="custom-control custom-checkbox"> | |
<input type="checkbox" class="custom-control-input" id="ledPower"> | |
<label class="custom-control-label" for="ledPower">Power leuchtet?</label> | |
</div> | |
<div class="input-group mb-3"> | |
<div class="input-group-prepend"> | |
<span class="input-group-text" id="inputGroup-result">Result</span> | |
</div> | |
<input type="text" class="form-control" id="result" aria-label="Sizing example input" aria-describedby="inputGroup-result"> | |
</div> | |
<button type="button" class="btn btn-secondary" id="senden">Senden</button> | |
<pre id="ergebnis"></pre> | |
</div> | |
<!-- Optional JavaScript --> | |
<!-- jQuery first, then Popper.js, then Bootstrap JS --> | |
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script> | |
<!-- <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script> --> | |
<!-- <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script> --> | |
<script> | |
$(function() { | |
$('button#senden').on('click', function() { | |
var obj = { | |
// Troubleshoot | |
troubleshoot: $('#troubleshoot').val(), | |
// LED | |
ledDSL: $('#ledDSL').is(':checked'), | |
ledPower: $('#ledPower').is(':checked') | |
} | |
var result = $('#result').val() | |
if (result) { // wenn das Feld nicht leer ist | |
// Hier wird eine neue Definition erstellt und in die Datenbank gespeichert | |
obj.result = result | |
$.post('http://localhost:8080/new', obj).done(function (data) { | |
console.log('Ergebnis:') | |
console.log(data) | |
}) | |
} else { | |
// Hier wird eine Definition abgefragt | |
$.post('http://localhost:8080/api', obj).done(function (data) { | |
console.log('Ergebnis:') | |
console.log(data) | |
$('#ergebnis').text(JSON.stringify(data)) | |
}) | |
} | |
}) | |
}) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment