Skip to content

Commit 78d8225

Browse files
authored
Add "Use tus-node-server with Fastify" to README (#244)
1 parent 3081f46 commit 78d8225

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,47 @@ const server = http.createServer((req, res) => {
119119
server.listen(port)
120120
```
121121

122+
#### Use tus-node-server with [Fastify](https://www.fastify.io)
123+
124+
```js
125+
const tus = require('tus-node-server');
126+
const tusServer = new tus.Server();
127+
tusServer.datastore = new tus.FileStore({
128+
path: '/files',
129+
});
130+
131+
const fastify = require('fastify')({ logger: true });
132+
133+
/**
134+
* add new content-type to fastify forewards request
135+
* without any parser to leave body untouched
136+
* @see https://www.fastify.io/docs/latest/Reference/ContentTypeParser/
137+
*/
138+
fastify.addContentTypeParser(
139+
'application/offset+octet-stream', async () => true
140+
);
141+
142+
/**
143+
* let tus handle preparation and filehandling requests
144+
* fastify exposes raw nodejs http req/res via .raw property
145+
* @see https://www.fastify.io/docs/latest/Reference/Request/
146+
* @see https://www.fastify.io/docs/latest/Reference/Reply/#raw
147+
*/
148+
fastify.all('/files', (req, res) => {
149+
tusServer.handle(req.raw, res.raw);
150+
});
151+
fastify.all('/files/*', (req, res) => {
152+
tusServer.handle(req.raw, res.raw);
153+
});
154+
155+
fastify.listen(3000, (err) => {
156+
if (err) {
157+
fastify.log.error(err);
158+
process.exit(1);
159+
}
160+
});
161+
```
162+
122163
## Features
123164
#### Events:
124165

0 commit comments

Comments
 (0)