Skip to content

Commit

Permalink
Keeping API Routing Clean Using Express Routers
Browse files Browse the repository at this point in the history
  • Loading branch information
Vincent Pr茅fontaine-Fournier committed Oct 5, 2018
1 parent a9d9cfd commit 0ac1a7f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 61 deletions.
66 changes: 5 additions & 61 deletions index.js
Expand Up @@ -5,69 +5,18 @@ const config = require('./config'),
app = express(),
cors = require('cors'),
bodyParser = require('body-parser'),
lfsa = require('./node_modules/lokijs/src/loki-fs-structured-adapter.js'),
loki = require('lokijs'),
db = new loki(config.DB_FILE, {
adapter: new lfsa(),
autoload: true,
autoloadCallback: initDb,
autosave: true,
autosaveInterval: 4000
}),
port = process.env.PORT || config.PORT,
debug = process.env.NODE_ENV === 'dev' || config.DEBUG
debug = process.env.NODE_ENV === 'dev' || config.DEBUG,
notebookRouter = require('./routers/notebook-router')


let notebooks;
let server;

app.use(express.static('public'))
app.use(bodyParser.json())

if (debug) app.use(cors())

app.head('/api/notebook/:id', (req, res) => {
if (!notebooks.findOne({ id: req.params.id })) return res.status(404).end()
res.status(200).end()
})

app.post('/api/notebook', (req, res) => {
if (!req.body || !req.body.id || !req.body.password) return res.status(400).end()
if (notebooks.findOne({ id: req.body.id })) return res.status(409).end()

let notebook = notebooks.insert({ id: req.body.id, password: req.body.password, notes: [] })
if (notebook) return res.status(201).send(notebook)
else return res.status(500).end()
})

app.get('/api/notebook/:id/notes', (req, res) => {
let notebook = notebooks.findOne({ id: req.params.id })
if (!notebook) return res.status(404).end()
if (req.get('Authorization') !== `Basic ${notebook.password}`) return res.status(401).end()
res.send(notebook.notes)
})

app.put('/api/notebook/:id/notes', (req, res) => {
if (!req.body || !Array.isArray(req.body)) return res.status(400).end()
let notebook = notebooks.findOne({ id: req.params.id })
if (!notebook) return res.status(404).end()
if (req.get('Authorization') !== `Basic ${notebook.password}`) return res.status(401).end()
notebook.notes = req.body
notebooks.update(notebook)
res.send(notebook.notes)
})

app.put('/api/notebook/:id/settings', (req, res) => {
if (!req.body) return res.status(400).end()
let notebook = notebooks.findOne({ id: req.params.id })
if (!notebook) return res.status(404).end()
if (req.get('Authorization') !== `Basic ${notebook.password}`) return res.status(401).end()

if (req.body.password) {
notebook.password = req.body.password;
}
notebooks.update(notebook)
res.send(notebook.notes)
})
app.use('/api', notebookRouter);

if (config.HTTPS_KEY && config.HTTPS_CERT) {
const https = require('https'),
Expand All @@ -88,9 +37,4 @@ if (!server) {

server.listen(port, () => {
console.log(`Listening on localhost:${port}.`)
})

function initDb() {
if (!db.getCollection(config.DB_COLLECTION_MAIN)) db.addCollection(config.DB_COLLECTION_MAIN)
notebooks = db.getCollection(config.DB_COLLECTION_MAIN)
}
})
69 changes: 69 additions & 0 deletions routers/notebook-router.js
@@ -0,0 +1,69 @@
'use strict'

const config = require('../config'),
lfsa = require('../node_modules/lokijs/src/loki-fs-structured-adapter'),
loki = require('lokijs'),
db = new loki(config.DB_FILE, {
adapter: new lfsa(),
autoload: true,
autoloadCallback: initDb,
autosave: true,
autosaveInterval: 4000
})

let notebooks;

function initDb() {
if (!db.getCollection(config.DB_COLLECTION_MAIN)) db.addCollection(config.DB_COLLECTION_MAIN)
notebooks = db.getCollection(config.DB_COLLECTION_MAIN)
}

const express = require('express'),
router = express.Router();


router.head('/notebook/:id', (req, res) => {
if (!notebooks.findOne({ id: req.params.id })) return res.status(404).end()
res.status(200).end()
})

router.post('/notebook', (req, res) => {
if (!req.body || !req.body.id || !req.body.password) return res.status(400).end()
if (notebooks.findOne({ id: req.body.id })) return res.status(409).end()

let notebook = notebooks.insert({ id: req.body.id, password: req.body.password, notes: [] })
if (notebook) return res.status(201).send(notebook)
else return res.status(500).end()
})

router.get('/notebook/:id/notes', (req, res) => {
let notebook = notebooks.findOne({ id: req.params.id })
if (!notebook) return res.status(404).end()
if (req.get('Authorization') !== `Basic ${notebook.password}`) return res.status(401).end()
res.send(notebook.notes)
})

router.put('/notebook/:id/notes', (req, res) => {
if (!req.body || !Array.isArray(req.body)) return res.status(400).end()
let notebook = notebooks.findOne({ id: req.params.id })
if (!notebook) return res.status(404).end()
if (req.get('Authorization') !== `Basic ${notebook.password}`) return res.status(401).end()
notebook.notes = req.body
notebooks.update(notebook)
res.send(notebook.notes)
})

router.put('/notebook/:id/settings', (req, res) => {
if (!req.body) return res.status(400).end()
let notebook = notebooks.findOne({ id: req.params.id })
if (!notebook) return res.status(404).end()
if (req.get('Authorization') !== `Basic ${notebook.password}`) return res.status(401).end()

if (req.body.password) {
notebook.password = req.body.password;
}
notebooks.update(notebook)
res.send(notebook.notes)
})

module.exports = router;

0 comments on commit 0ac1a7f

Please sign in to comment.