From dc76231f1416bc5fd59d0cccdebbbc511b949854 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Wed, 24 Jun 2020 11:00:44 +0200 Subject: [PATCH] server --- server/.gitignore | 7 +++++++ server/package.json | 16 ++++++++++++++++ server/src/index.js | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 server/.gitignore create mode 100644 server/package.json create mode 100644 server/src/index.js diff --git a/server/.gitignore b/server/.gitignore new file mode 100644 index 0000000..f64c4af --- /dev/null +++ b/server/.gitignore @@ -0,0 +1,7 @@ +# Node stuff +yarn.lock +package-lock.json +node_modules + +# DB +*.db \ No newline at end of file diff --git a/server/package.json b/server/package.json new file mode 100644 index 0000000..2d5772f --- /dev/null +++ b/server/package.json @@ -0,0 +1,16 @@ +{ + "scripts": { + "dev": "node-dev src", + "start": "node src" + }, + "dependencies": { + "body-parser": "^1.19.0", + "cors": "^2.8.5", + "express": "^4.17.1", + "nedb": "^1.8.0", + "nedb-promise": "^2.0.1" + }, + "devDependencies": { + "node-dev": "^4.0.0" + } +} diff --git a/server/src/index.js b/server/src/index.js new file mode 100644 index 0000000..1cd99b2 --- /dev/null +++ b/server/src/index.js @@ -0,0 +1,38 @@ +const express = require('express') +const cors = require('cors') +const { json } = require('body-parser') +const Datastore = require('nedb-promise') + +const db = new Datastore({ filename: './todos.db', autoload: true }) + +const app = express() +app.use(cors()) +app.use(json()) +const port = 8000 + +app.get('/todo', async (req, res) => { + const todos = await db.find({}) + res.send(todos) +}) + +app.post('/todo', async (req, res) => { + const saved = await db.insert(req.body) + res.send(saved) +}) + +app.delete('/todo/:id/', async (req, res) => { + await db.remove({ _id: req.params.id }) + res.status(200).end() +}) + +app.post('/todo/:id/', async (req, res) => { + await db.update({ _id: req.params.id }, req.body) + res.status(200).end() +}) + +app.get('*', (req, res) => res.status(404).send('Not Found')) + +app.listen(port, () => { + db.find({}, console.log) + console.log(`Example app listening at http://localhost:${port}`) +})