【问题标题】:Deno run is not working properly also drunDeno 运行不正常也醉了
【发布时间】:2020-05-27 00:34:26
【问题描述】:

在创建了一个 index.ts 并编写了一个简单的代码来监听端口 3000 并在 body 上打印 hello world 之后,我也无法运行或获取 deno 的 drun 模块的输出.

import { Application, Router } from "https://deno.land/x/denotrain@v0.5.0/mod.ts";

const app = new Application();

const router = new Router();

// Middleware 
app.use((ctx) => {

  ctx.cookies["user.session"] = "qwertz";
  ctx.cookies["a"] = "123";
  ctx.cookies["b"] = "456";
  delete ctx.cookies["user.session"];
  return;
});

router.get("/", (ctx) => {

  return new Promise((resolve) => resolve("This is the admin interface!")); 
});
router.get("/edit", async (ctx) => {
  return "This is an edit mode!"; 
});

app.get("/", (ctx) => {

  return {"hello": "world"};
});

app.use("/admin", router);

app.get("/:id", (ctx) => {
  // Use url parameters
  return "Hello World with ID: " + ctx.req.params.id
});

  return ctx.req.body;
});

await app.run()

【问题讨论】:

  • 运行时得到什么输出?你用什么命令来运行服务器?
  • 顺便说一句,您发布的代码有语法错误。
  • deno run --allow-net index.ts 是我编写的运行应用程序的命令
  • 我用 drun 来运行应用程序但没有用

标签: deno


【解决方案1】:

开发环境:- Windows 10

问题似乎是地址 0.0.0.0 仅特定于 ma​​c。Windows 不使用 0.0.0.0 地址。

转到 localhost:3000 / 127.0.0.1:3000 后。我能够得到输出。我想也许 Windows 将 0.0.0.0 重定向到本地主机。无论如何它解决了我的问题!

【讨论】:

    【解决方案2】:

    我在窗户上。我遇到了同样的问题。那么,

    const app = new Application({hostname:"127.0.0.1"});
    

    我在打字稿中创建了应用程序,并提供了上面的参数主机名。 然后像这样运行 deno:

    deno run --allow-net=127.0.0.1 index.ts
    

    成功了。

    【讨论】:

      【解决方案3】:

      使用以下命令运行您的服务器:

      drun watch --entryPoint=./server.ts --runtimeOptions=--allow-net
      

      无论如何,大多数用于监视更改的 Deno 工具仍然存在错误,我建议使用 nodemon,并带有 --exec 标志

      nodemon --exec deno run --allow-net server.ts
      

      为方便起见,您可以使用nodemon.json,内容如下:

      {
        "execMap": {
          "js": "deno run --allow-net",
          "ts": "deno run --allow-net"
        },
        "ext": "js,json,ts"
      }
      

      现在只需使用:nodemon server.ts

      【讨论】:

      • 我试过你之前发布的drun命令仍然没有结果。但是在0.0.0.0:8000上显示服务
      • 该应用默认使用3000 显示您设置端口的位置:8000
      • 如果您在没有drunnodemon 的情况下运行它,它是否有效,只需deno run -A server.ts
      • 对不起,我用的是3000。如果是端口问题,我改成8000。
      • 运行 deno run 命令后,它仍然显示我上面显示的相同错误。什么都没有显示。
      【解决方案4】:

      看来你的代码 sn-p 有错误,最后一个

        return ctx.req.body;
      });
      

      如果您修复该问题并使用最新的 deno (v1.0.1) 和 drun(v1.1.0) 版本,它应该可以使用以下命令: drun --entryPoint=index.ts --runtimeOptions=--allow-net

      【讨论】:

        猜你喜欢
        • 2021-11-30
        • 2019-03-29
        • 2010-09-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-06
        • 2021-07-19
        相关资源
        最近更新 更多