tus / tus-node-server

Node.js tus server, standalone or integrable in any framework, with disk, S3, Azure, and GGC stores.
https://tus.io/
MIT License
824 stars 201 forks source link

Integrate tus into Express Example Not Working #527

Closed stormbard closed 11 months ago

stormbard commented 11 months ago

The example code block in the docs is as follows

const {Server} = require('@tus/server')
const {FileStore} = require('@tus/file-store')
const express = require('express')

const host = '127.0.0.1'
const port = 1080
const app = express()
const uploadApp = express()
const server = new Server({
  datastore: new FileStore({directory: '/files'}),
})

uploadApp.all('*', server.handle.bind(server))
app.use('/uploads', uploadApp)
app.listen(port, host)

If I copy as I get the following error Error: 'path' is not defined; must have a path. Adding a path such that it looks like this and it works. The docs should probably be updated

const {Server} = require('@tus/server')
const {FileStore} = require('@tus/file-store')
const express = require('express')

const host = '127.0.0.1'
const port = 1080
const app = express()
const uploadApp = express()
const server = new Server({
  path: '/',
  datastore: new FileStore({directory: '/files'}),
})

uploadApp.all('*', server.handle.bind(server))
app.use('/uploads', uploadApp)
app.listen(port, host)

Why define the path on the tus server since it is ultimately being handled by express? Why not either pull it from the incoming request instead of the config options? As is it means I have to update the path in two locations if I ever decide to change its path.

Murderlon commented 11 months ago

Good catch. We need to define a path because the server can also be used standalone. But regardless of that, it makes sense to only accept a path for resumable uploads and have the rest 404 instead of blindly accepting any path by inferring it from the request.