server.js 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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('/sessions/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('/sessions/whoami', (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. {} // this EP should return meta data of the workspace (id, description, label, slug, sidebar_entries (?)
  47. ))
  48. server.get('/workspace/:idws/contents/', (req, res) => {
  49. console.log(req.query)
  50. if (req.query.parent_id !== undefined) { // get content of a folder
  51. switch (req.query.parent_id) {
  52. case '3':
  53. return res.jsonp(jsonDb.folder_content_3)
  54. case '11':
  55. return res.jsonp(jsonDb.folder_content_11)
  56. }
  57. } else { // get content of a workspace
  58. return res.jsonp(
  59. Object.assign(
  60. {},
  61. jsonDb.workspace_detail,
  62. {
  63. content: shuffle(jsonDb.workspace_detail.content.map(
  64. c => Object.assign({}, c, {workspace_id: req.params.idws})
  65. ))
  66. },
  67. {id: req.params.idws}
  68. )
  69. )
  70. }
  71. })
  72. server.get('/user/:id/roles', (req, res) => res.jsonp(jsonDb.user_role))
  73. server.get('/timezone', (req, res) => res.jsonp(timezoneDb.timezone))
  74. server.get('/workspace/:idws/contents/:idc', (req, res) => {
  75. switch (req.params.idc) {
  76. case '1': // pageHtml
  77. case '5':
  78. return res.jsonp(jsonDb.content_data_pageHtml)
  79. case '2':
  80. return res.jsonp(jsonDb.content_data_thread)
  81. case '6':
  82. return res.jsonp({})
  83. }
  84. })
  85. server.get('/workspace/:idws/contents/:idc/timeline', (req, res) => {
  86. switch (req.params.idc) {
  87. case '1': // pageHtml
  88. case '5':
  89. return res.jsonp(jsonDb.timeline)
  90. case '2':
  91. return res.jsonp([])
  92. case '6': // File
  93. return res.jsonp(jsonDb.timeline)
  94. }
  95. })
  96. server.use(router)
  97. server.listen(GLOBAL_PORT, () => {
  98. console.log('JSON Server is running on port : ' + GLOBAL_PORT)
  99. })
  100. /*
  101. Object.keys(req) :
  102. ['_readableState', 'readable', 'domain', '_events', '_eventsCount', '_maxListeners', 'socket', 'connection',
  103. 'httpVersionMajor', 'httpVersionMinor', 'httpVersion', 'complete', 'headers', 'rawHeaders', 'trailers', 'rawTrailers',
  104. 'upgrade', 'url', 'method', 'statusCode', 'statusMessage', 'client', '_consuming', '_dumped', 'next', 'baseUrl',
  105. 'originalUrl', '_parsedUrl', 'params', 'query', 'res', '_parsedOriginalUrl', '_startAt', '_startTime', '_remoteAddress',
  106. 'body', 'route' ]
  107. */