Skip to content
Snippets Groups Projects
Commit 7017591b authored by Matias Meri's avatar Matias Meri
Browse files

backend

parent b00a6546
No related branches found
No related tags found
No related merge requests found
index.js 0 → 100644
const express = require("express")
const app = express()
const PORT = 3001;
const bodyParser = require("body-parser")
let reminders = {
reminders: [
{
name: "Buy some eggs",
timestamp: "2021-11-11T00:00:00.141Z",
id: 1
},
{
name: "Make an omelette",
timestamp: "2021-11-11T00:00:00.141Z",
id: 2
},
{
name: "Wash dishes",
timestamp: "2021-11-11T00:00:00.141Z",
id: 3
},
{
name: "Buy more eggs",
timestamp: "2021-11-11T00:00:00.141Z",
id: 4
}
]
}
app.get("/",(req, res) => {
res.send("<h1>Hello World!</h1>")
})
app.get("/api/reminders", (req, res) => {
res.json(reminders)
})
app.get("/api/reminders/:id", (req, res) => {
const id = Number(req.params.id)
const reminder = reminders.reminders.find(reminder => reminder.id === id)
if(reminder) {
res.json(reminder)
} else {
res.status(404).end()
}
})
app.delete("/api/reminders/:id",(req, res) =>{
const id = Number(req.params.id)
reminders = reminders.reminders.filter(reminder => reminder.id !== id);
res.status(204).end()
})
app.use(bodyParser.json())
app.post('/api/reminders/', (request, response) => {
const body = request.body
if(body.name === undefined || body.timestamp === undefined){
console.log('Error')
response.status(401).json({error: "Required info missing"})
return
}
if(reminders.reminders.some(reminder => reminder.name === body.name)){
console.log('Reminder already exists')
response.status(401).json({error: "Reminder already exists"})
return
}
const newReminder = {
name: String(body.name),
timestamp: Date.now(),
id: Math.floor(Math.random() * 100000)
}
response.json(newReminder)
})
app.listen(PORT, function(err) {
if (err) console.log(err)
console.log("Server listening on PORT", PORT)
})
This diff is collapsed.
{
"name": "wmo_backend",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js",
"watch": "nodemon index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Matias Meri",
"license": "ISC",
"dependencies": {
"express": "^4.17.1",
"nodemon": "^2.0.7"
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment