BOLL
BOLL•2w ago

FileServer from tutorial, 2000+ ms to connect?

Hello! I used this guide as an example, and made a small file server.
export interface IHttpServerOptions {
name: string
port: number
rootFolders: IStringDictionary
}

export default class HttpServer {
private readonly TAG: string
private readonly _options: IHttpServerOptions

constructor(options: IHttpServerOptions) {
this._options = options
this.start()
}

private start() {
Deno.serve(
{hostname: 'localhost', port: this._options.port},
(request) => {
const pathName = new URL(request.url).pathname
const pair =
Object.entries(this._options.rootFolders)
.find(([key, value]) => {
return pathName.startsWith(key)
})
const rootPath = pair && pair.length == 2 ? `${pair[1]}` : ''
if (rootPath.length) {
return serveDir(request, {
fsRoot: rootPath
})
} else {
console.log('Unable to match path to static file store', request.url)
}
return new Response()
}
)
}
}
export interface IHttpServerOptions {
name: string
port: number
rootFolders: IStringDictionary
}

export default class HttpServer {
private readonly TAG: string
private readonly _options: IHttpServerOptions

constructor(options: IHttpServerOptions) {
this._options = options
this.start()
}

private start() {
Deno.serve(
{hostname: 'localhost', port: this._options.port},
(request) => {
const pathName = new URL(request.url).pathname
const pair =
Object.entries(this._options.rootFolders)
.find(([key, value]) => {
return pathName.startsWith(key)
})
const rootPath = pair && pair.length == 2 ? `${pair[1]}` : ''
if (rootPath.length) {
return serveDir(request, {
fsRoot: rootPath
})
} else {
console.log('Unable to match path to static file store', request.url)
}
return new Response()
}
)
}
}
My problem is that when I load files from it, most of the requests takes 2000+ms to connect. The transfer itself is fast, just a few ms, but the initial connection has this big delay. I'm not sure what I have done that might cause this 😅
Deno
Write a file server
In-depth documentation, guides, and reference materials for building secure, high-performance JavaScript and TypeScript applications with Deno
No description
3 Replies
BOLL
BOLLOP•2w ago
Deno.serve(
{hostname: 'localhost', port: this._options.port},
(request) => {
return serveDir(request, {fsRoot: '../web/dist'})
}
)
Deno.serve(
{hostname: 'localhost', port: this._options.port},
(request) => {
return serveDir(request, {fsRoot: '../web/dist'})
}
)
Simplified it to this to remove all of my own logic, but it still has the same timing... 🥹 It seems when the page has been cached, it loads quickly, which is unsurprising. But whenever forcing a refresh it gets this timing back. I'll try a different browser too. Oh... the issue is only in Firefox... I shall investigate.
bartlomieju
bartlomieju•2w ago
Are you on Windows? Try changing localhost to 127.0.0.1
BOLL
BOLLOP•7d ago
Yes, windows! I'll do this when I get home, didn't have energy yesterday to debug much after bumping into this. Header values all looked OK so I was a bit out of ideas. 😅 I finally tried this, and with the local IP it works super 😄 I would probably never have figured this out, so big thanks 🤣