server.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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) =>
  41. // res.jsonp({"logged": false})
  42. res.jsonp(jsonDb.user_logged)
  43. )
  44. server.get('/user/:id/workspace', (req, res) => res.jsonp(jsonDb.workspace_list))
  45. server.get('/workspace/:id', (req, res) => res.jsonp(
  46. Object.assign(
  47. {},
  48. jsonDb.workspace_detail,
  49. {content: shuffle(jsonDb.workspace_detail.content.map(
  50. c => Object.assign({}, c, {workspace_id: req.params.id})
  51. ))},
  52. {id: req.params.id}
  53. )
  54. ))
  55. server.get('/workspace/:idws/folder/:idf', (req, res) => {
  56. switch (req.params.idf) {
  57. case '3':
  58. return res.jsonp(jsonDb.folder_content_3)
  59. case '11':
  60. return res.jsonp(jsonDb.folder_content_11)
  61. }
  62. })
  63. server.get('/user/:id/roles', (req, res) => res.jsonp(jsonDb.user_role))
  64. server.get('/timezone', (req, res) => res.jsonp(timezoneDb.timezone))
  65. server.get('/workspace/:idws/content/:idc', (req, res) => {
  66. switch (req.params.idc) {
  67. case '1': // pageHtml
  68. return res.jsonp(jsonDb.content_data_pageHtml)
  69. case '2':
  70. return res.jsonp(jsonDb.content_data_thread)
  71. }
  72. })
  73. server.get('/workspace/:idws/content/:idc/timeline', (req, res) => {
  74. switch (req.params.idc) {
  75. case '1': // pageHtml
  76. return res.jsonp(jsonDb.timeline)
  77. case '2':
  78. return res.jsonp([])
  79. }
  80. })
  81. server.use(router)
  82. server.listen(GLOBAL_PORT, () => {
  83. console.log('JSON Server is running on port : ' + GLOBAL_PORT)
  84. })