Deno Render HTML Files

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Hey guys, so we got started with Deno, and created a Deno API. But what if we want Deno to server actual files?
Today we'll be looking into Deno as a server option.
In Node, you probably have heard of Express. This was the middle layer to render files. In Deno, we have something similar called Oak.
First we are going start by importing the modules we need.
import {Application} from 'https://deno.land/x/[email protected]/mod.ts';
import {
viewEngine,
engineFactory,
adapterFactory
} from 'https://deno.land/x/[email protected]/mod.ts';
Now we must define our adapters.
const ejsEngine = engineFactory.getEjsEngine();
const oakAdapter = adapterFactory.getOakAdapter();
We are choosing the ejs template but we can also use handlebars or denjucks.
Then we define our oakAdapter.
Now, we are going to start our application:
const app = new Application();
app.use(viewEngine(oakAdapter, ejsEngine));
Here we define a new Deno application and tell it to use the defined view engine.
We aren't using routes for this example, but will just return a one time view:
app.use(async (ctx, next) => {
ctx.render('index.ejs', {data: {msg: 'Tips'}});
});
See the data attribute? We are going to pass a variable to our view, which ejs can render for us.
Now, all we have to do is spool up our app on port 8000.
await app.listen({port: 8000});
As for our ejs file, We are using a plain bootstrap starter:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
<!-- Bootstrap CSS -->
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous"
/>
<title>Deno Server</title>
</head>
<body>
<h1>Daily Dev <%= data.msg %></h1>
</body>
</html>
Now are are ready too run our server by executing:
deno run --allow-net --allow-read server.ts
Now open a browser and locate: http://localhost:8000 and see our site!
Find this project on GitHub.
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter