From 24fb4fb6babe88cc8b3e6969c43da54dae2d736e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9e=20Kooi?= Date: Wed, 31 Mar 2021 13:46:27 +0200 Subject: [PATCH] docs: companion should be mounted on a subpath --- website/src/docs/companion.md | 48 +++++++++++++++++------------------ 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/website/src/docs/companion.md b/website/src/docs/companion.md index 8f491f2db..79bd5d8fa 100644 --- a/website/src/docs/companion.md +++ b/website/src/docs/companion.md @@ -50,56 +50,54 @@ Companion may either be used as a pluggable express app, which you plug into you ### Plugging into an already existing server -To plug Companion into an existing server, call its `.app` method, passing in an [options](#Options) object as a parameter. +To plug Companion into an existing server, call its `.app` method, passing in an [options](#Options) object as a parameter. This returns a server instance that you can mount on a subpath in your Express or app. -```javascript +```js +const express = require('express') +const bodyParser = require('body-parser') +const session = require('express-session') +const companion = require('@uppy/companion') -var express = require('express') -var bodyParser = require('body-parser') -var session = require('express-session') -var companion = require('@uppy/companion') +const app = express() -var app = express() +// Companion requires body-parser and express-session middleware. +// You can add it like this if you use those throughout your app. +// +// If you are using something else in your app, you can add these +// middlewares in the same subpath as Companion instead. app.use(bodyParser.json()) app.use(session({secret: 'some secrety secret'})) -... -// be sure to place this anywhere after app.use(bodyParser.json()) and app.use(session({...}) + const options = { providerOptions: { drive: { key: 'GOOGLE_DRIVE_KEY', - secret: 'GOOGLE_DRIVE_SECRET' - } + secret: 'GOOGLE_DRIVE_SECRET', + }, }, server: { host: 'localhost:3020', protocol: 'http', + // This MUST match the path you specify in `app.use()` below: + path: '/companion', }, - filePath: '/path/to/folder/' + filePath: '/path/to/folder/', } -app.use(companion.app(options)) - -``` - -please be sure to allow the following HTTP methods in your server like so: - -```javascript -res.header("Access-Control-Allow-Methods", "OPTIONS, GET, POST, PATCH, PUT"); +app.use('/companion', companion.app(options)) ``` See [Options](#Options) for valid configuration options. -To use WebSockets for realtime upload progress, you can call the `socket` method, like so: +Then, add the Companion WebSocket server for realtime upload progress, using the `companion.socket` function: -```javascript -... -var server = app.listen(PORT) +```js +const server = app.listen(PORT) companion.socket(server, options) ``` -This takes your `server` instance and your Uppy [Options](#Options) as parameters. +This takes your `server` instance and [Options](#Options) as parameters. ### Running as a standalone server