20

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Nanda
  • 236
  • 2
  • 8
  • 2
    I might be wrong but arent Deno and NodeJS two different systems? They are not the same right (even though both allow you to run Javascript outside your browser)? – Mathyn May 29 '20 at 08:25
  • 1
    @Mathyn, Yes both are entirely different but they are javascript I don't know how to use them in production mode. – Nanda May 29 '20 at 08:28
  • 1
    @Mathyn NodeJS and deno, both use V8 engine by chrome as a runtime to run Javascript. – Ayman Arif May 29 '20 at 08:28
  • 1
    Compared to other server setups Node and Deno actually are similar...if you want to spin up a server you need to install the Deno runtime on your server machine, then add your project code and run it... – bloo May 29 '20 at 08:30
  • 1
    @AshwynHorton Deno was created by NodeJS creator himself. He wanted to create a secure runtime as well as a better package manager solution (npm is a big ball of mud) – Ayman Arif May 29 '20 at 08:34
  • Here is how you can install https://deno.land/manual/getting_started/installation, and here is a base tutorial to get started on an API, https://www.freecodecamp.org/news/the-deno-handbook#your-first-deno-app-for-real- – bloo May 29 '20 at 08:35

4 Answers4

17

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

Marcos Casagrande
  • 37,983
  • 8
  • 84
  • 98
11

You can just use:

pm2 start index.ts --interpreter="deno" --interpreter-args="run --allow-net" 
Aral Roca
  • 5,442
  • 8
  • 47
  • 78
1

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"]
James Wright
  • 3,000
  • 1
  • 18
  • 27
0

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.

NetOperator Wibby
  • 1,354
  • 5
  • 22
  • 44