Let’s say I have some static files I want to serve.
For example, a React app or a Svelte app.
Create Svelte App
Let’s use the Svelte template:
npx degit sveltejs/template svelte-app
cd svelte-app
npm install
Now we need to create a production build:
npm run build
The command will create an optimized build in the public
folder.
Create Deno Server
We’ll use the third-party module Abc to create a simple web server.
Let’s create a new file. Name it mod.ts
or mod.js
. We don’t add any types, so it doesn’t matter if you use vanilla JavaScript or TypeScript:
import { Application } from 'https://deno.land/x/abc@v1.0.0-rc10/mod.ts'
const app = new Application()
app.static('/', 'public').file('/', 'public/index.html').start({ port: 8080 })
app.static
registers a new route on the root path and serves files from the public
folder.
app.file
serves the actual HTML file.
app.start
starts the server on port 8080.
Documentation is available on deno.land.
Now you can run the file with Deno. You have to specify the permissions you give Deno:
deno run --allow-net="0.0.0.0:8080" --allow-read mod.ts
You have to allow network access on your local host with the port. Deno must also be able to read the static files you want to serve.