server.js 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. const jsonServer = require('json-server')
  2. const jsonDb = require('./static_db.json')
  3. const timezoneDb = require('./timezone.json')
  4. const server = jsonServer.create()
  5. const router = jsonServer.router() // for persistence : jsonServer.router('static_db.json')
  6. const middlewares = jsonServer.defaults()
  7. const GLOBAL_PORT = 3001
  8. function shuffle(array) {
  9. let currentIndex = array.length
  10. let randomIndex
  11. // While there remain elements to shuffle...
  12. while (currentIndex !== 0) {
  13. // Pick a remaining element...
  14. randomIndex = Math.floor(Math.random() * currentIndex)
  15. currentIndex -= 1
  16. // And swap it with the current element.
  17. ;[array[randomIndex], array[currentIndex]] = [array[currentIndex], array[randomIndex]]
  18. }
  19. return array;
  20. }
  21. server.use(middlewares)
  22. server.use(jsonServer.bodyParser)
  23. // res.jsonp(req.query)
  24. server.get('/echo', (req, res) => res.jsonp('gg'))
  25. server.get('/login', (req, res) => res.jsonp(jsonDb.login))
  26. server.get('/user_logged', (req, res) => res.jsonp(jsonDb.user_logged))
  27. server.delete('/deletenodata', (req,res) => res.status(204).jsonp(''))
  28. server.patch('/user', (req, res) => res.jsonp({lang: 'fr'}))
  29. // server.put('/api/data/raw_materials_vendors/:vendorid', (req, res) => {
  30. // res.jsonp(jsonVendorColorData.vendorVariableData)
  31. // console.log(req.body)
  32. // res.jsonp('gg')
  33. // })
  34. server.get('/lang', (req, res) => res.jsonp(jsonDb.lang))
  35. server.post('/user/login', (req, res) => {
  36. if (req.body.login !== '' && req.body.password !== '') return res.jsonp(jsonDb.user_logged)
  37. else return res.jsonp('error')
  38. })
  39. server.get('/app/config', (req, res) => res.jsonp(jsonDb.app_config))
  40. server.get('/user/is_logged_in', (req, res) => res.jsonp(jsonDb.user_logged))
  41. server.get('/user/:id/workspace', (req, res) => res.jsonp(jsonDb.workspace_list))
  42. server.get('/workspace/:id', (req, res) => res.jsonp(
  43. // {...jsonDb.workspace_detail, content: shuffle(jsonDb.workspace_detail.content)})
  44. Object.assign({}, jsonDb.workspace_detail, {content: shuffle(jsonDb.workspace_detail.content)}, {id: req.params.id})
  45. ))
  46. server.get('/user/:id/roles', (req, res) => res.jsonp(jsonDb.user_role))
  47. server.get('/timezone', (req, res) => res.jsonp(timezoneDb.timezone))
  48. server.get('/workspace/:idws/content/:idc', (req, res) => {
  49. switch (req.params.idc) {
  50. case '1': // pageHtml
  51. return res.jsonp(jsonDb.content_data_pageHtml)
  52. case '2':
  53. return res.jsonp(jsonDb.content_data_thread)
  54. }
  55. })
  56. server.get('/workspace/:idws/content/:idc/timeline', (req, res) => {
  57. switch (req.params.idc) {
  58. case '1': // pageHtml
  59. return res.jsonp(jsonDb.timeline)
  60. case '2':
  61. return res.jsonp([])
  62. }
  63. })
  64. server.use(router)
  65. server.listen(GLOBAL_PORT, () => {
  66. console.log('JSON Server is running on port : ' + GLOBAL_PORT)
  67. })