server.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const fs = require('fs')
  2. const path = require('path')
  3. const rimraf = require('rimraf')
  4. const companion = require('../../packages/@uppy/companion')
  5. const app = require('express')()
  6. const DATA_DIR = path.join(__dirname, 'tmp')
  7. app.use(require('cors')({
  8. origin: true,
  9. credentials: true
  10. }))
  11. app.use(require('cookie-parser')())
  12. app.use(require('body-parser').json())
  13. app.use(require('express-session')({
  14. secret: 'hello planet'
  15. }))
  16. const options = {
  17. providerOptions: {
  18. google: {
  19. key: process.env.COMPANION_GOOGLE_KEY,
  20. secret: process.env.COMPANION_GOOGLE_SECRET
  21. },
  22. s3: {
  23. getKey: (req, filename) =>
  24. `whatever/${Math.random().toString(32).slice(2)}/${filename}`,
  25. key: process.env.COMPANION_AWS_KEY,
  26. secret: process.env.COMPANION_AWS_SECRET,
  27. bucket: process.env.COMPANION_AWS_BUCKET,
  28. region: process.env.COMPANION_AWS_REGION,
  29. endpoint: process.env.COMPANION_AWS_ENDPOINT
  30. }
  31. },
  32. server: { host: 'localhost:3020' },
  33. filePath: DATA_DIR,
  34. secret: 'blah blah',
  35. debug: true
  36. }
  37. // Create the data directory here for the sake of the example.
  38. try {
  39. fs.accessSync(DATA_DIR)
  40. } catch (err) {
  41. fs.mkdirSync(DATA_DIR)
  42. }
  43. process.on('exit', function () {
  44. rimraf.sync(DATA_DIR)
  45. })
  46. app.use(companion.app(options))
  47. const server = app.listen(3020, () => {
  48. console.log('listening on port 3020')
  49. })
  50. companion.socket(server, options)