mirror of
https://github.com/cupcakearmy/svelte-rest-demo.git
synced 2024-12-21 23:56:26 +00:00
server
This commit is contained in:
parent
42176ca4fd
commit
dc76231f14
7
server/.gitignore
vendored
Normal file
7
server/.gitignore
vendored
Normal file
@ -0,0 +1,7 @@
|
||||
# Node stuff
|
||||
yarn.lock
|
||||
package-lock.json
|
||||
node_modules
|
||||
|
||||
# DB
|
||||
*.db
|
16
server/package.json
Normal file
16
server/package.json
Normal 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
38
server/src/index.js
Normal 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}`)
|
||||
})
|
Loading…
Reference in New Issue
Block a user