Nanda
Nanda

Reputation: 236

How to use deno in production

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.

Upvotes: 20

Views: 7456

Answers (4)

NetOperator Wibby
NetOperator Wibby

Reputation: 1404

Just wanted to share the command I use

pm2 start main.ts --interpreter="deno" --interpreter-args="run --allow-env --allow-net --allow-read --unstable --no-prompt" --name "my-cool-app" -- start --production

You already know the Deno flags: --allow-env, --allow-net, --allow-read, --unstable.

--no-prompt disables Deno from asking if you want to enable flags that something in your app requires and you forgot to allow.

--name is for pm2. When you run pm2 list or pm2 status, you see the name of your app, instead of some generic name.

--production is just an extra flag I gave my application.


Thanks to Aral's answer for putting me on the right track.

Upvotes: 0

James Wright
James Wright

Reputation: 3010

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"]

Upvotes: 1

Aral Roca
Aral Roca

Reputation: 5919

You can just use:

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

Upvotes: 11

Marcos Casagrande
Marcos Casagrande

Reputation: 40414

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

Upvotes: 20

Related Questions