This commit is contained in:
cupcakearmy 2020-06-24 11:00:44 +02:00
parent 42176ca4fd
commit dc76231f14
No known key found for this signature in database
GPG Key ID: D28129AE5654D9D9
3 changed files with 61 additions and 0 deletions

7
server/.gitignore vendored Normal file
View File

@ -0,0 +1,7 @@
# Node stuff
yarn.lock
package-lock.json
node_modules
# DB
*.db

16
server/package.json Normal file
View File

@ -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"
}
}

38
server/src/index.js Normal file
View File

@ -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}`)
})