An easy guide to serving files in Deno with the serveFile method

After compiling my script, which runs without any errors, it's supposed to serve index.html. However, when the page is loading, nothing is being sent to the browser.

import { serve } from "https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="394c5a4afeeff1e3fbf0fef">[email protected]</a>/http/server.ts";
import { serveFile } from 'https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="12474b5541405e49514e50">[email protected]</a>/http/file_server.ts';

const server = serve({ port: 8000 });
console.log("http://localhost:8000/");

for await (const req of server) {
  console.log(req.url);
  if(req.url === '/')
    await serveFile(req, 'index.html');
}

Can anyone explain why serveFile isn't functioning properly in this particular case?

Answer №1

Upon calling the serveFile function, a Response object (containing status, headers, body) is created but not yet sent.

In order to send the response, you need to make a separate call to req.respond():

import { serve } from "https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f6858292b6c6d8cfc7d8c6">[email protected]</a>/http/server.ts";
import { serveFile } from 'https://deno.land/<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="bac9cedefa8a94838b948a">[email protected]</a>/http/file_server.ts';

const server = serve({ port: 8000 });
console.log("http://localhost:8000/");

for await (const req of server) {
  console.log(req.url);
  if(req.url === '/') {
    const response = await serveFile(req, 'index.html');
    req.respond(response)
  }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Vite in Vue 3 does not stop build process for errors

Encountered the error message "TS2322: Type 'number' is not assignable to type 'string'." Instead of fixing it in the code, I prefer to disable it. Using "vue-tsc --noEmit && vite build" for my build in package.json. Currently workin ...

Javascript/Typescript Performance Evaluation

I am looking to create a visual report in the form of a table that displays the count of each rating based on the date. The ratings are based on a scale of 1 to 5. Below is the object containing the data: [ { "Date": "01/11/2022", ...

Is it possible to fix all parameters using @Injectable as well as @Inject?

Recently, as I've been making the switch to Angular 5, I encountered an error with my ApiService that I can't seem to resolve: can't resolve all parameters for ApiService(?) Oddly enough, this issue only started cropping up after I introduc ...

Unable to retrieve values from nested objects in component.html

Hey fellow tech enthusiasts, I'm currently dealing with a complex nested JSON object retrieved from an API. Here's a snippet of the object: profile : { title:"Mr", personalInfo:{ fullNames: "John Doe", id ...

The FOR UPDATE clause is not functioning as intended in the SELECT query

I have been working on isolating my database to prevent multiple servers from reading or updating data in the same row. In order to achieve this, I have structured my query like so: SELECT * FROM bridge_transaction_state as bridge WHERE bridge.state IN (&a ...

Organizing objects into arrays in Node.js

I have an array and I need to concatenate an object after the array. Here is my array: const users = [ {name: "Joe", age: 22}, {name: "Kevin", age: 24}, {name: "Peter", age: 21} ] And here is my object: ...

Challenges encountered while running the npm start command

I'm diving into some Angular tutorials and having trouble getting npm to run on OSX Yosemite. Here's the error log. I've followed the instructions, but still can't view the compiled app at localhost:3000. 0 info it works if it ends wi ...

Challenge encountered with TypeScript integration in the controller

I am currently in the process of converting a website from VB to C# and incorporating TypeScript. I have successfully managed to send the data to the controller. However, instead of redirecting to the next page, the controller redirects back to the same pa ...

Tips for launching a new page with a button click in React.js without relying on Routers

I am completely new to React and trying to figure out how to dynamically load a new page component within the main page without changing the URL. Essentially, I want to open up a fresh page upon clicking a button on the main page, displaying only the conte ...

What is the best way to set up environments for Google App Engine (GAE

After developing a web app with server and client components, I decided to deploy it to Google Cloud using App Engine. Although the deployment process was successful, the main issue lies in the non-functioning env_variables that are crucial for the applic ...

How can I avoid a component from triggering its subscription twice in Angular 5 when the component is nested twice inside another component?

How can I prevent a component in Angular 5 from triggering its subscription twice when the component is placed twice inside another component? For example, I have a NavMenuComponent with two instances of a cart in its template. <!-- cart 1 in n ...

Having numerous repetitions of identical modals in Ionic 2/3

My app.component contains a background mode service that shares data via intent to a behavior Subject. this._notification.setNotiService2(data.extras); After logging in, the root is set to TabsPage. this.appCtrl.getRootNav().setRoot('TabsPage' ...

My Angular-based todo application has encountered an error notification from the system

Every time I try to post something, the system responds with a 405 error message in the console. I'm not sure what caused this issue or how to resolve it. Alternatively, if I click the done button, the console displays a 500 error message. Here is t ...

Can you explain the mechanics behind the functionalities of @angular and @type dependencies?

This inquiry may have been raised before, but I couldn't uncover all the solutions. If that's the case, my apologies. I have a good grasp on how package.json and dependencies / dev-dependencies function in Node applications. Currently delving i ...

Can Vitest be configured to use a separate tsconfig file?

Can I specify a different tsconfig file for vitest? In my extensive project that is currently using vue-cli, I am considering switching to vite. However, before making the full conversion, I would like to start by using vitest instead of jest. The issue a ...

The argument representing 'typeof Store' cannot be assigned to the parameter representing 'Store<AppState>'

I'm encountering an issue while trying to expand a service in Angular that utilizes ngrx. The error message I'm receiving is as follows: Argument of type 'typeof Store' is not assignable to parameter of type 'Store<AppState>& ...

What is the best way to locate the desired number from a group of three numbers in JavaScript?

Is there a way to determine if "x" is equal to any of the numbers 15, 30, 70, or 140? const x =....; if(x===?){....}else{....} ...

The addition of special characters to strings in TypeScript through JavaScript is not functioning as expected

I need assistance on conditionally appending a string based on values from captured DOM elements. When the value is empty, I want to include the special character "¬". However, when I try adding it, I get instead because the special character is not reco ...

Tips for retrieving information from a Vuetify modal window

Is it possible to retrieve data from a dialog in the VueJS Vuetify framework? Specifically, how can I access the username or password entered in the NewUserPopup.vue dialog from app.vue? App.vue = main template NewUserPopup.vue = Dialog template imported ...

"Optimize Your Data with PrimeNG's Table Filtering Feature

I'm currently working on implementing a filter table using PrimeNG, but I'm facing an issue with the JSON structure I receive, which has multiple nested levels. Here's an example: { "id": "123", "category": "nice", "place": { "ran ...