index.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const express = require('express')
  2. const bodyParser = require('body-parser')
  3. const session = require('express-session')
  4. const companion = require('@uppy/companion')
  5. const app = express()
  6. app.use(bodyParser.json())
  7. app.use(session({
  8. secret: 'some-secret',
  9. resave: true,
  10. saveUninitialized: true,
  11. }))
  12. app.use((req, res, next) => {
  13. res.setHeader('Access-Control-Allow-Origin', req.headers.origin || '*')
  14. next()
  15. })
  16. // Routes
  17. app.get('/', (req, res) => {
  18. res.setHeader('Content-Type', 'text/plain')
  19. res.send('Welcome to Companion')
  20. })
  21. // initialize uppy
  22. const companionOptions = {
  23. providerOptions: {
  24. drive: {
  25. key: 'your google key',
  26. secret: 'your google secret',
  27. },
  28. instagram: {
  29. key: 'your instagram key',
  30. secret: 'your instagram secret',
  31. },
  32. dropbox: {
  33. key: 'your dropbox key',
  34. secret: 'your dropbox secret',
  35. },
  36. box: {
  37. key: 'your box key',
  38. secret: 'your box secret',
  39. },
  40. // you can also add options for additional providers here
  41. },
  42. server: {
  43. host: 'localhost:3020',
  44. protocol: 'http',
  45. },
  46. filePath: './output',
  47. secret: 'some-secret',
  48. debug: true,
  49. }
  50. const { app: companionApp } = companion.app(companionOptions)
  51. app.use(companionApp)
  52. // handle 404
  53. app.use((req, res) => {
  54. return res.status(404).json({ message: 'Not Found' })
  55. })
  56. // handle server errors
  57. app.use((err, req, res) => {
  58. console.error('\x1b[31m', err.stack, '\x1b[0m')
  59. res.status(err.status || 500).json({ message: err.message, error: err })
  60. })
  61. companion.socket(app.listen(3020))
  62. console.log('Welcome to Companion!')
  63. console.log(`Listening on http://0.0.0.0:${3020}`)