Skip to content

Instantly share code, notes, and snippets.

@picpoint
Created June 22, 2019 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save picpoint/102011b4700aba7ec405c303e1d44869 to your computer and use it in GitHub Desktop.
Save picpoint/102011b4700aba7ec405c303e1d44869 to your computer and use it in GitHub Desktop.
Simple server to express
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 ...');
});
[
{
"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