MCPcopy Create free account
hub / github.com/callstackincubator/react-native-fast-io / fetch

Function fetch

example/server/createServer.ts:8–45  ·  view source on GitHub ↗
(req, server)

Source from the content-addressed store, hash-verified

6export function createServer(payload: string | Bun.BufferSource, port: number) {
7 const server = Bun.serve({
8 async fetch(req, server) {
9 // Handle WebSocket upgrade
10 if (server.upgrade(req)) {
11 return
12 }
13
14 // Handle POST /upload
15 if (req.method === 'POST' && req.url.endsWith('/upload')) {
16 try {
17 console.log('Waiting for body...')
18
19 if (!req.body) {
20 return new Response('No body provided', { status: 400 })
21 }
22
23 const writer = Bun.file('uploaded_file.jpg.gz').writer()
24
25 for await (const chunk of req.body) {
26 // Write each chunk to file
27 writer.write(chunk)
28 // Debug
29 console.log('Chunk saved:', chunk.length, 'bytes')
30 }
31
32 await writer.end()
33
34 console.log('Upload complete, file saved')
35
36 return new Response('Upload successful', { status: 200 })
37 } catch (error) {
38 console.error('Upload error:', error)
39 return new Response('Upload failed', { status: 500 })
40 }
41 }
42
43 // Default response for other routes
44 return new Response('Not found', { status: 404 })
45 },
46 websocket: {
47 message(ws, message: string) {
48 if (typeof message !== 'string') {

Callers

nothing calls this directly

Calls 1

writeMethod · 0.65

Tested by

no test coverage detected