Created
June 22, 2019 10:48
-
-
Save picpoint/102011b4700aba7ec405c303e1d44869 to your computer and use it in GitHub Desktop.
Simple server to express
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const express = require('express'); | |
const app = express(); | |
const todos = require('./todos.json'); | |
app.get('/', function (req, res) { | |
res.send('Home Page'); | |
}); | |
app.get('/todos', (req, res) => { | |
if (req.query.completed) { | |
return res.json(todos.filter(todo => todo.completed.toString() === req.query.completed)); | |
} | |
res.json(todos); | |
}); | |
app.get('/todos/:id', (req, res) => { | |
let todo = todos.find(todo => { | |
return todo.id == req.params.id; | |
}); | |
if (!todo) { | |
res.status(404).send('Page not found .... :-( '); | |
} else { | |
res.json(todo); | |
} | |
}); | |
app.listen(3000, () => { | |
console.log('server is starting ...'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{ | |
"id": 1, | |
"title": "Learn Nodejs", | |
"completed": true | |
}, | |
{ | |
"id": 2, | |
"title": "Learn Express", | |
"completed": false | |
}, | |
{ | |
"id": 3, | |
"title": "Learn MongoDB", | |
"completed": false | |
} | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment