【问题标题】:Give back answers from backend based on inputs from frontend根据前端的输入从后端返回答案
【发布时间】:2022-01-27 23:31:23
【问题描述】:

每当“你好”作为前端的输入时,我想从后端向前端返回一个答案,如果输入不是“你好”,则不应返回任何内容

import { opine } from "https://deno.land/x/opine@2.1.1/mod.ts";
import { opineCors } from "https://deno.land/x/cors/mod.ts";
const app = opine();
app.use(opineCors())

app.get("/gibmirdieloesung", function (req, res) {
    console.log(require("Hallo").inspect(req.params))
    res.send({ answer: "This is an example" });
});

app.listen(
    3004,
    () => console.log("server has started on http://localhost:3004 ????"),
);

require() 的用法正确吗?如果没有,您有什么建议吗?

谢谢你

【问题讨论】:

  • 查看:stackoverflow.com/questions/63205191/…查看解决方案
  • “来自前端的输入”是什么意思?
  • 抱歉造成误会。嗯基本上我试图建立一个网络应用程序。这是后端的代码。它重新识别来自网络应用程序的输入并返回答案或解决方案。在这种情况下,“这是一个例子”。我们通过分析解决了这个问题:stackoverflow.com/questions/63205191/…。尽管如此,谢谢你:)
  • 我假设您使用 GET 方法提交表单,因此我的回答。有趣的“gibmirdieloesung”,输入 gib mir die loesung (de_DE) 太长了。

标签: typescript svelte deno


【解决方案1】:

我删除了requireinspect 并稍微更改了响应。

现在当使用下面的代码请求http://localhost:3004/hello?a=1&b=2

#!/usr/bin/env deno run --allow-read --allow-net

import { opine } from "https://deno.land/x/opine@2.1.1/mod.ts";
import { opineCors } from "https://deno.land/x/cors/mod.ts";
const app = opine();
app.use(opineCors())

app.get("/hello", function (req, res) {
    console.log(req.query);
    let r = req.query;
    r.answer = "This is an example";
    res.send(r);
});

app.listen(
    3004,
    () => console.log("server has started on http://localhost:3004 ?"),
);

我收到{"a":"1","b":"2","answer":"This is an example"} 作为回复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    • 2021-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多