简体   繁体   English

如何在生产中使用 deno

[英]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.我在本地 pc 中尝试了Deno ( https://deno.land/ ) 及其一些示例,我们必须在本地环境中执行 API 之前运行服务器。

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.我需要将它托管在服务器中,以便在我们请求时调用 API,但我不知道该怎么做。

I have the experience in hosting PHP,.NET in production mode i haven't used Nodejs yet so i don't know that process.我有在生产模式下托管PHP,.NET的经验,我还没有使用过 Nodejs ,所以我不知道这个过程。

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.您可以使用您喜欢的云提供商 AWS、DigitalOcean、Azure... 并安装deno ,然后您可以使用带有interpreter标志的pm2在服务器崩溃和/或在启动时启动服务器时自动重启。

The easiest way is to create an ecosystem.config.js最简单的方法是创建一个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 .并使用interpreterArgs将您需要传递给deno的 arguments 传递。

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.如果您愿意,也可以使用 Nginx 作为反向代理。

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, Kube.netes or even just Docker running on a static Linux machine if a container orchestration platform is excessive for your particular needs.您可以考虑使用官方denoland/deno Docker 图像将您的应用程序容器化,您可以将其部署到 AWS Fargate、Kube.netes 之类的应用程序,如果容器编排平台无法满足您的特定需求,您甚至可以将其部署在 static Linux 机器上运行的 Docker . Here's a Dockerfile I wrote based upon said image for a Deno microservice :这是我根据上述图像为Deno 微服务编写的 Dockerfile:

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

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 .您已经知道 Deno 标志:--allow --allow-env 、-- --allow.net 、--allow --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. --no-prompt禁止 Deno 询问您是否要启用您的应用程序中某些东西需要但您忘记允许的标志。

--name is for pm2. --name用于 pm2。 When you run pm2 list or pm2 status , you see the name of your app, instead of some generic name.当您运行pm2 listpm2 status时,您会看到应用程序的名称,而不是一些通用名称。

--production is just an extra flag I gave my application. --production只是我给应用程序的一个额外标记。


Thanks to Aral 's answer for putting me on the right track.感谢Aral回答让我走上正轨。

You can use tools that NodeJS ecosystem provides to host Node applications:您可以使用 NodeJS 生态系统提供的工具来托管 Node 应用程序:

  1. Netlify : Tool that can deploy your node application that you commit. Netlify :可以部署您提交的节点应用程序的工具。 Uses concepts of Gitops to trigger automated builds.使用 Gitops 的概念来触发自动构建。

  2. Surge

  3. Zeit Now/Vercel现在时代/Vercel

Note these examples are for static sites.请注意,这些示例适用于 static 站点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM