“Http.post”如何执行以及代码路径是什么?

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

How does this Http.post execute and what is the code path?

问题

I understand that you want a translation of the code part without any additional information. Here's the translation:

// Login user
document.addEventListener('click', (event) => {
  event.preventDefault();

  if (event.target.matches('#login-btn')) {
    console.log('Login button was pressed')
    logger.info('Login button was pressed')
    var emailInput = document.getElementById('email-input');
    var pwdInput = document.getElementById('pwd-input');
    var data = {
      email: emailInput.value,
      password: pwdInput.value,
    };
    Http
      .post('/api/auth/login', data) // What does this do and how does it do it?
      .then(() => {
        window.location.href = '/users';
      });
  }
}, false);

Please note that the code you provided contains placeholders like Http, logger, and certain HTML elements. The translation retains these placeholders in their original form.

英文:

I'm learning web development and I don't understand how the Http.post section works. This is from an ExpressJS with Typescript repo I found on Github. When the user clicks the login button on the site, this login.js script handles click events, right? What exactly is happening when the server does Http.post('/api/auth/login', data)? Is Http a built-in function in NodeJS? If I look in the directory for this repo, I don't see a /api/auth/login folder, so where is this execution going? Ultimately this logs the user in, but I just don't see the code execution path here.

login.js:

// Login user
document.addEventListener('click', (event) => {
  event.preventDefault();

  if (event.target.matches('#login-btn')) {
    console.log('Login button was pressed')
    logger.info('Login button was pressed')
    var emailInput = document.getElementById('email-input');
    var pwdInput = document.getElementById('pwd-input');
    var data = {
      email: emailInput.value,
      password: pwdInput.value,
    };
    Http
      .post('/api/auth/login', data) // What does this do and how does it do it?
      .then(() => {
        window.location.href = '/users';
      });
  }
}, false);

答案1

得分: 1

似乎 Http 在 ./lib/project-files-auth/src/public/scripts 下的 http.js 文件中被定义。

该对象是一个结构,具有与函数相关联的 post 属性。

该函数使用标准函数 fetch 发送一个 post 请求到 '/api/auth/login',调用是异步的,fetch 返回一个 promise,因此当 post 请求返回结果时,您可以调用 next 并更新窗口对象。

英文:

It seems Http is defined in http.js under ./lib/project-files-auth/src/public/scripts

The object is a structure with a post attribute associated with a function.

The function is using the standard function fetch to send a post request to '/api/auth/login', the call is asynchronous, fetch returns a promise so you can call next and update the window object when the post request has returned something.

huangapple
  • 本文由 发表于 2023年6月30日 04:26:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584408.html
匿名

发表评论

匿名网友

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

确定