调用 Express 控制器内的 ‘document / redirect’ API。

huangapple go评论62阅读模式
英文:

Call 'document / redirect' API inside express controller

问题

我有以下API调用的document / redirect,如下所示:

调用 Express 控制器内的 ‘document / redirect’ API。

它在HTML代码中被调用,如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>Test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <meta http-equiv="Content-Language" content="en-us" />
  </head>
  <body>
    <form
      name="autoForm"
      action="http://test/Integration.htm"
      method="post"
    >
      <input type="submit" name="submit" value="Continue" />
      <input type="input" name="TEST_VALUE" value="test" />
    </form>
  </body>
</html>

现在我想在Express控制器中实现相同的逻辑。我需要实现一个控制器,调用这个外部API http://test/Integration.htm。流程将如下:

前端 -> Express控制器 -> 调用 http://test/Integration.htm -> 重定向前端页面

我尝试在Express中构建以下控制器,但没有成功:

export const ssoHandler: any = async (req: Request, res: Response) => {
  const formData = new URLSearchParams();

  const clientCandidateId = req.query.clientCandidateId as string;

  formData.append('TEST_VALUE', clientCandidateId);

  const response = await axios({
    url: `${config.host}`,
    headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
    method: 'POST',
    data: formData,
  });

  res.write(response.data);
};

但没有成功。我尝试在控制器中构建自定义HTML并发送回客户端。但我收到了CORS错误。而且我不认为这是正确的方法。

是否有办法在Express中使用相同的方法?由于安全原因,我不能在前端代码中实现此要求。我必须在控制器中执行这个。

英文:

I have document / redirect API call as shown below

调用 Express 控制器内的 ‘document / redirect’ API。

It's being called inside HTML Code like this

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
  &lt;head&gt;
    &lt;title&gt;Test&lt;/title&gt;
    &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot; /&gt;
    &lt;meta http-equiv=&quot;Content-Language&quot; content=&quot;en-us&quot; /&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;form
      name=&quot;autoForm&quot;
      action=&quot;http://test/Integration.htm&quot;
      method=&quot;post&quot;
    &gt;
      &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Continue&quot; /&gt;
      &lt;input type=&quot;input&quot; name=&quot;TEST_VALUE&quot; value=&quot;test&quot; /&gt;
    &lt;/form&gt;
  &lt;/body&gt;
&lt;/html&gt;

Now i want to implement the same logic within express controller. I need to implement a contorller which calls this external API http://test/Integration.htm. The flow will be like this:-

Front-end -> Express controller -> Call the http://test/Integration.htm -> Redirect the front-end page

I tried to build the below controller inside express

export const ssoHandler: any = async (req: Request, res: Response) =&gt; {
  const formData = new URLSearchParams();

  const clientCandidateId = req.query.clientCandidateId as string;

  formData.append(&#39;TEST_VALUE&#39;, clientCandidateId);

  const response = await axios({
    url: `${config.host}`,
    headers: { &#39;Content-Type&#39;: &#39;application/x-www-form-urlencoded&#39; },
    method: &#39;POST&#39;,
    data: formData,
  });

  res.write(response.data)
};

But no luck. I tried bulding a custom HTML insisde the controller and sending back to client. But i received CORS error. Also i don't believe this is the right approach.

Is there any way to make the same approach using express ?. For security reasons. I can't implement this requirement inside the front-end code. I have to do it inside controller like this.

答案1

得分: 1

CORS错误基本上是跨平台阻止/预防。您可以通过运行命令 "npm i cors" 通过CLI 安装 cors,然后在您的 server.js 或 index.js 文件中写入:

const app = express();
app.use(cors);

希望这可以帮助解决问题。谢谢和问候。

英文:

Basically CORS error refers to cross platform blockage/prevention. You may install cors through cli by running command "npm i cors". and then in your server.js or index.js file write:

const app=express();
app.use(cors);

I hope this will help to solve. Thanks & regards.

huangapple
  • 本文由 发表于 2023年5月18日 13:42:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/76278039.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定