Last active
May 16, 2017 16:45
-
-
Save Symfomany/ff47f7a587e888529930389ac0c9d675 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
let express = require('express') | |
let app = express() | |
let datas = require('./datas.json') | |
let fs = require('fs'); | |
var cors = require('cors'); | |
/******** | |
* | |
* Utilisez Nodemon.js | |
* https://nodemon.io/ | |
* | |
*********/ | |
// use it before all route definitions | |
app.use(cors({ origin: 'http://localhost:8080' })); | |
/** | |
****************************************** API ************************************************ | |
*/ | |
/** | |
* "/" Home | |
*/ | |
app.get('/', function (req, res) { | |
res.json(datas); | |
}); | |
/** | |
* Limit de à | |
*/ | |
app.get('/:debut-:fin', function (req, res) { | |
let debut = req.params.debut; | |
let fin = req.params.fin; | |
let tab = datas.splice(debut, fin); | |
res.json(tab); | |
}); | |
/** | |
* "/send": Add a posts | |
*/ | |
app.post('/', function (req, res) { | |
res.json(datas); | |
}); | |
/** | |
* "/visible" visible | |
*/ | |
app.get('/visible', function (req, res) { | |
let visibles = datas.filter((task) => task.visible == true); | |
res.json(visibles); | |
}); | |
/** | |
* "star" >= 7 | |
*/ | |
app.get('/stars', function (req, res) { | |
let stars = datas.filter((task) => task.visible >= stars); | |
return stars | |
}); | |
/** | |
* Route search | |
*/ | |
app.get('/search/:word', function (req, res) { | |
let word = req.params.word; | |
if (!word) { | |
res.json(datas); | |
} | |
let regex = new RegExp(word, 'i') | |
let filtrer = datas.filter((task) => regex.test(task.title)); | |
res.json(filtrer); | |
}); | |
/** | |
* Visibilite | |
*/ | |
app.get('/visibilite/:visible', function (req, res) { | |
let visibility = req.params.visible; | |
let filtrer = datas.filter((task) => task.visible == visibility); | |
res.json(filtrer); | |
}); | |
/** | |
* Change title | |
*/ | |
app.get('/change-title/:id/:title', function (req, res) { | |
let id = req.params.id; | |
let title = decodeURIComponent(req.params.title); // parse Boolean | |
let post = datas.find((elt) => elt.id == id); // get reference of datas | |
post.title = title; | |
let json = JSON.stringify(datas); | |
fs.writeFile('datas.json', json, 'utf8', () => { | |
res.json(datas); | |
}); | |
}); | |
app.get('/change-visibility/:id/:visible', function (req, res) { | |
let id = req.params.id; | |
let visible = req.params.visible === "true"; // parse Boolean | |
let post = datas.find((elt) => elt.id == id); // get reference of datas | |
post.visible = visible; | |
let json = JSON.stringify(datas); | |
fs.writeFile('datas.json', json, 'utf8', () => { | |
res.json(datas); | |
}); | |
}); | |
/** | |
* Add post | |
*/ | |
app.get('/post', function (req, res) { | |
datas.push({ | |
"id": datas.length + 1, | |
"title": "Test...", | |
"visible": true, | |
"note": 8 | |
}); | |
let json = JSON.stringify(datas); | |
fs.writeFile('datas.json', json, 'utf8', () => { | |
res.json(datas); | |
}); | |
}); | |
app.listen(3000, function () { | |
console.log('Le serveur des taches est lancé!') | |
}) | |
This file contains hidden or 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": "Blablablabla...", | |
"category": "Sport", | |
"visible": false, | |
"note": 2 | |
}, | |
{ | |
"id": 2, | |
"title": "ici...", | |
"category": "Sport", | |
"visible": false, | |
"note": 5 | |
}, | |
{ | |
"id": 3, | |
"title": "lala...", | |
"category": "Insolite", | |
"visible": true, | |
"note": 7 | |
}, | |
{ | |
"id": 4, | |
"title": "Test...", | |
"category": "Insolite", | |
"visible": true, | |
"note": 8 | |
}, | |
{ | |
"id": 5, | |
"title": "Test...", | |
"category": "Insolite", | |
"visible": true, | |
"note": 8 | |
}, | |
{ | |
"id": 6, | |
"title": "Test...", | |
"category": "Economie", | |
"visible": true, | |
"note": 8 | |
}, | |
{ | |
"id": 7, | |
"title": "Test...", | |
"category": "Economie", | |
"visible": true, | |
"note": 8 | |
}, | |
{ | |
"id": 8, | |
"title": "Alpha...", | |
"category": "Cinema", | |
"visible": false, | |
"note": 6 | |
}, | |
{ | |
"id": 9, | |
"title": "Beta...", | |
"category": "Cinema", | |
"visible": true, | |
"note": 10 | |
}, | |
{ | |
"id": 10, | |
"title": "coucou tu veux", | |
"category": "People", | |
"visible": true, | |
"note": 8 | |
}, | |
{ | |
"id": 11, | |
"title": "Omega...", | |
"category": "People", | |
"visible": true, | |
"note": 2 | |
}, | |
{ | |
"id": 12, | |
"title": "Test...", | |
"visible": true, | |
"note": 8 | |
}, | |
{ | |
"id": 13, | |
"title": "Test...", | |
"visible": true, | |
"note": 8 | |
}, | |
{ | |
"id": 14, | |
"title": "Test...", | |
"visible": true, | |
"note": 8 | |
}, | |
{ | |
"id": 15, | |
"title": "Test...", | |
"visible": true, | |
"note": 8 | |
}, | |
{ | |
"id": 16, | |
"title": "Test...", | |
"visible": true, | |
"note": 8 | |
}, | |
{ | |
"id": 17, | |
"title": "Test...", | |
"visible": true, | |
"note": 8 | |
}, | |
{ | |
"id": 18, | |
"title": "Test...", | |
"visible": true, | |
"note": 8 | |
} | |
] |
This file contains hidden or 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
{ | |
"dependencies": { | |
"cors": "^2.8.3", | |
"express": "^4.15.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment