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

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

Call 'document / redirect' API inside express controller

问题

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

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

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

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <title>Test</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
  6. <meta http-equiv="Content-Language" content="en-us" />
  7. </head>
  8. <body>
  9. <form
  10. name="autoForm"
  11. action="http://test/Integration.htm"
  12. method="post"
  13. >
  14. <input type="submit" name="submit" value="Continue" />
  15. <input type="input" name="TEST_VALUE" value="test" />
  16. </form>
  17. </body>
  18. </html>

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

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

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

  1. export const ssoHandler: any = async (req: Request, res: Response) => {
  2. const formData = new URLSearchParams();
  3. const clientCandidateId = req.query.clientCandidateId as string;
  4. formData.append('TEST_VALUE', clientCandidateId);
  5. const response = await axios({
  6. url: `${config.host}`,
  7. headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
  8. method: 'POST',
  9. data: formData,
  10. });
  11. res.write(response.data);
  12. };

但没有成功。我尝试在控制器中构建自定义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

  1. &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;
  2. &lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
  3. &lt;head&gt;
  4. &lt;title&gt;Test&lt;/title&gt;
  5. &lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=ISO-8859-1&quot; /&gt;
  6. &lt;meta http-equiv=&quot;Content-Language&quot; content=&quot;en-us&quot; /&gt;
  7. &lt;/head&gt;
  8. &lt;body&gt;
  9. &lt;form
  10. name=&quot;autoForm&quot;
  11. action=&quot;http://test/Integration.htm&quot;
  12. method=&quot;post&quot;
  13. &gt;
  14. &lt;input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Continue&quot; /&gt;
  15. &lt;input type=&quot;input&quot; name=&quot;TEST_VALUE&quot; value=&quot;test&quot; /&gt;
  16. &lt;/form&gt;
  17. &lt;/body&gt;
  18. &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

  1. export const ssoHandler: any = async (req: Request, res: Response) =&gt; {
  2. const formData = new URLSearchParams();
  3. const clientCandidateId = req.query.clientCandidateId as string;
  4. formData.append(&#39;TEST_VALUE&#39;, clientCandidateId);
  5. const response = await axios({
  6. url: `${config.host}`,
  7. headers: { &#39;Content-Type&#39;: &#39;application/x-www-form-urlencoded&#39; },
  8. method: &#39;POST&#39;,
  9. data: formData,
  10. });
  11. res.write(response.data)
  12. };

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 文件中写入:

  1. const app = express();
  2. 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:

确定