server.js 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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.put('/workspaces/:idws/html-documents/:idcts', (req, res) => {
  86. return res.jsonp(jsonDb.putHtmlDoc)
  87. })
  88. server.get('/workspace/:idws/contents/:idc/timeline', (req, res) => {
  89. switch (req.params.idc) {
  90. case '1': // pageHtml
  91. case '5':
  92. return res.jsonp(jsonDb.timeline)
  93. case '2':
  94. return res.jsonp([])
  95. case '6': // File
  96. return res.jsonp(jsonDb.timeline)
  97. }
  98. })
  99. server.post('/workspaces/:idws/contents', (req, res) => {
  100. console.log(req.body)
  101. return res.jsonp('gg')
  102. })
  103. server.use(router)
  104. server.listen(GLOBAL_PORT, () => {
  105. console.log('JSON Server is running on port : ' + GLOBAL_PORT)
  106. })
  107. /*
  108. Object.keys(req) :
  109. ['_readableState', 'readable', 'domain', '_events', '_eventsCount', '_maxListeners', 'socket', 'connection',
  110. 'httpVersionMajor', 'httpVersionMinor', 'httpVersion', 'complete', 'headers', 'rawHeaders', 'trailers', 'rawTrailers',
  111. 'upgrade', 'url', 'method', 'statusCode', 'statusMessage', 'client', '_consuming', '_dumped', 'next', 'baseUrl',
  112. 'originalUrl', '_parsedUrl', 'params', 'query', 'res', '_parsedOriginalUrl', '_startAt', '_startTime', '_remoteAddress',
  113. 'body', 'route' ]
  114. */