WBE_Praktikum_7/code/index.js

123 lines
2.5 KiB
JavaScript
Raw Permalink Normal View History

2022-11-24 11:10:59 +01:00
/**
* Webservice mit Express
* WBE-Praktikum
*/
var express = require('express')
var app = express()
// Fehlerobjekt anlegen
//
function error(status, msg) {
var err = new Error(msg)
err.status = status
return err
}
// Zufällige ID erzeugen, Quelle:
// https://stackoverflow.com/questions/6860853/generate-random-string-for-div-id#6860916
//
function guidGenerator() {
var S4 = function() {
return (((1+Math.random())*0x10000)|0).toString(16).substring(1)
}
return (S4()+S4()+"-"+S4()+"-"+S4()+"-"+S4()+"-"+S4()+S4()+S4())
}
// Statische Dateien im Verzeichnis public
app.use(express.static('public'))
// API-Key überprüfen
//
app.use('/api', function(req, res, next){
var key = req.query['api-key']
// Key fehlt
if (!key) {
return next(error(400, 'api key required'))
}
// Key falsch
if (!~apiKeys.indexOf(key)) {
return next(error(401, 'invalid api key'))
}
// korrekter Key
req.key = key
next()
})
// JSON-Daten akzeptieren
app.use(express.json())
// gültige API-Keys
var apiKeys = ['wbeweb', 'c4game']
2022-11-24 21:06:23 +01:00
2022-11-24 11:10:59 +01:00
// unsere tolle in-memory Datenbank :)
2022-11-24 21:06:23 +01:00
const columns = 7
const rows = 6
var data = {}
2022-11-24 11:10:59 +01:00
// GET-Request bearbeiten
//
2022-11-24 21:06:23 +01:00
app.get('/', function(req, res, next){
console.log("received default GET request")
res.sendFile('public/connect4.html', { root: __dirname })
})
2022-11-24 11:10:59 +01:00
app.get('/api/data/:id', function(req, res, next){
2022-11-24 21:06:23 +01:00
console.log("received API get request")
2022-11-24 11:10:59 +01:00
var id = req.params.id
var result = data[id]
if (result) res.send(result)
else next()
})
// POST-Request bearbeiten
//
app.post('/api/data', function (req, res, next) {
2022-11-24 21:06:23 +01:00
console.log("received POST request")
var id = guidGenerator()
2022-11-24 11:10:59 +01:00
data[id] = req.body
res.send({id})
})
// DELETE-Request bearbeiten
//
app.delete('/api/data/:id', function(req, res, next){
var id = req.params.id
delete data[id]
res.sendStatus(204)
})
// PUT-Request bearbeiten
//
app.put('/api/data/:id', function(req, res, next){
var id = req.params.id
if (data[id]) {
data[id] = req.body
res.send(req.body)
}
else next()
})
// Middleware mit vier Argumenten wird zur Fehlerbehandlung verwendet
//
app.use(function(err, req, res, next){
res.status(err.status || 500)
res.send({ error: err.message })
})
// Catch-all: wenn keine vorangehende Middleware geantwortet hat, wird
// hier ein 404 (not found) erzeugt
//
app.use(function(req, res){
res.status(404)
res.send({ error: "not found" })
})
app.listen(3000)
console.log('Express started on port 3000')