How to build an environment for Deno's Oak framework
overview
I will explain various environment construction methods of Oak, a Deno-based web framework.
In this article, we will introduce how to build the environment in the following three ways.
- How to build environment with core module
- How to build an environment with Docker
This section describes how to build an Oak environment using the following versions.
Oak v8.0.0
Deno v1.16.0
Also, all the code I’ve created so far is listed below. https://github.com/wiblok/Deno-Oak
***Oak works with Deno. If you don’t have Deno installed, download it from the official website and install it. ** Deno
How to build environment with core module
This method imports and uses Oak’s core modules. Please follow the steps below.
To build an environment with Oak alone, the following prerequisites must be met.
Create project directory
First, create a new directory to create your Oak project. Run the below command in command line
mkdir oak-core-project
Change to project directory
Go to your project directory. Please run the following command:
cd oak-core-project
After moving, move to the root directory of the project.
Creating an application
Create an Oak application. Create a new file (e.g. app.ts
) inside your project directory and add the following code
app.ts
import { Application } from "https://deno.land/x/oak@v8.0.0/mod.ts";
const app = new Application();
app. use((ctx) => {
ctx.response.body = "Hello, Oak!";
});
await app.listen({ port: 8000 });
The example above shows the basic structure of an Oak application. Define a handler for the root path using intermediate processing functions and
Returns Hello, Oak!
as the object.
The final directory structure will look like this:
oak-core-project
├── app.ts
Start the server
Finally, start the Oak server. Please run the below command
deno run --allow-net app.ts
If the server starts successfully, you should see “Server started on port 8000” in the console. Point your browser to http://localhost:8000 to verify that your Oak application is working properly.
How to build environment with Docker
Deno can also build an environment using Docker.
First, install Docker. Download the Docker installer for your operating system from the official Docker website and install it.
Create project directory
First, create a new directory to create the Deno project. Run the following command on the command line:
mkdir deno-docker-project
Change to project directory
Go to the created project directory. Please run the below command
cd deno-docker-project
Creating a DockerFile
# Use the official Deno Docker image as the base image
FROM denoland/deno:1.14.0
# Set the working directory inside the container
WORKDIR /app
# Copy the application files to the container
COPY..
# Allow network access for the Deno application
ENV DENO_FLAGS="--allow-net=0.0.0.0:3000"
# Specify the command to run the Deno application with Oak
CMD ["deno", "run", "--unstable", "--allow-net", "app.ts"]
Creating an application
Create a Deno application. Create a new file (e.g. app.ts
) inside your project directory and add the following code:
app.ts
import { Application } from "https://deno.land/x/oak@v8.0.0/mod.ts";
const app = new Application();
app. use((ctx) => {
ctx.response.body = "Hello, Oak!";
});
await app.listen({ port: 3000 });
The example above uses Deno and Oak to show the basic structure of the application. Use Router
to define a handler for the route path and return Hello, Deno!
as a response.
The final directory structure will look like this:
oak-core-project
├── app.ts
|── Dockerfile
Building a Docker image
Build the Docker image using the command:
docker build -t deno-docker-project .
Running a Docker container
Run a Docker container from the built Docker image:
docker run -p 3000:3000 -d deno-docker-project
This will run your Deno application inside a Docker container and map it to port 3000 on your host. You can access the Deno application by accessing http://localhost:3000 from your browser, API test tool, etc.
The above is the procedure for building a Deno application environment using Docker. By using Docker, you can reduce the work of building the environment and make the development process smoother. Note: Access to port 3000 may be restricted by the host’s firewall or network settings. Adjust your firewall and network settings as needed.