server.js 1.4 KB

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