How to use deno in production

Viewed 4405

I tried Deno (https://deno.land/) in local pc and some of its examples and we have to run the server before executing the APIs in the local environment.

I need to host it in the server so that I can call that API when we request, but i don't know how to do it.

I have the experience in hosting PHP,.NET in production mode i haven't used Nodejs yet so i don't know that process.

4 Answers

You can use the cloud provider of your preference, AWS, DigitalOcean, Azure... and install deno and then you can use pm2 using interpreter flag to automatically restart if the server crashes and/or start the server on boot.

The easiest way is to create an ecosystem.config.js

module.exports = {
  apps: [
    {
      name: "app",
      script: "./deno.js",
      interpreter: "deno",
      interpreterArgs: "run --allow-net --allow-read",
    },
  ],
};

And use interpreterArgs to pass arguments that you need to pass to deno.

Now all you need to do is:

pm2 start

Now your server will be available on whatever port you setup your server. You can use Nginx as a reverse proxy if you want too.

You can also use any process manager of your preference

You can just use:

pm2 start index.ts --interpreter="deno" --interpreter-args="run --allow-net" 

You could consider containerising your application with the official denoland/deno Docker image, which you can deploy to the likes of AWS Fargate, Kubernetes or even just Docker running on a static Linux machine if a container orchestration platform is excessive for your particular needs. Here's a Dockerfile I wrote based upon said image for a Deno microservice:

# Production Dockerfile that caches
# project dependencies at build time

FROM denoland/deno:1.15.3

ARG postgres_host
ARG postgres_user
ARG postgres_password
ARG postgres_db
ARG postgres_pool_connections

COPY . /microservice
WORKDIR /microservice
USER deno

ENV POSTGRES_HOST=$postgres_host
ENV POSTGRES_USER=$postgres_user
ENV POSTGRES_PASSWORD=$postgres_password
ENV POSTGRES_DB=$postgres_db
ENV POSTGRES_POOL_CONNECTIONS=$postgres_pool_connections

RUN ["deno", "cache", "deps.ts"]
EXPOSE 8000
CMD ["run", "--allow-env", "--allow-net", "service/server.ts"]

You can use tools that NodeJS ecosystem provides to host Node applications:

  1. Netlify: Tool that can deploy your node application that you commit. Uses concepts of Gitops to trigger automated builds.

  2. Surge

  3. Zeit Now/Vercel

Note these examples are for static sites.

Related