繁体   English   中英

如何运行节点脚本

[英]How to run a Node script

我需要能够运行节点脚本以从外部API删除对象。 因此,我应该能够运行以下命令:

node server.js Customer55555

并且它应该删除对象。

我已经使用Axios调用了API。

const axios = require("axios");

const API = "http://dummy.restapiexample.com/api/v1/employees";
function getAllEmployees() {
  axios
    .get("http://dummy.restapiexample.com/api/v1/employees")
    .then(response => {
      //   console.log(response.data);
      console.log(response.status);
      function filterEmployee() {
        const employeeData = response.data;
        employeeData.filter(employee => {
          console.log(employee);
        });
        // console.log(employeeData);
      }
      filterEmployee();
    })
    .catch(error => {
      console.log(error);
    });
}

function deleteEmployee() {
  axios({
    method: "DELETE",
    url: "http://dummy.restapiexample.com/api/v1/delete/36720",
    headers: { "Content-Type": "application/json" }
  })
    .then(
      // Observe the data keyword this time. Very important
      // payload is the request body
      // Do something
      console.log("user deleted")
    )
    .catch(function(error) {
      // handle error
      console.log(error);
    });
}

// getAllEmployees();
deleteEmployee();

我可以得到一个单独的对象,但是我需要弄清楚如何通过运行上面的命令来删除它。

您可以执行以下操作:

const axios = require("axios")

const API = "http://dummy.restapiexample.com/api/v1/employees"

async function getAllEmployees(filter = null) {
  try {
    const response = await axios.get("http://dummy.restapiexample.com/api/v1/employees")
    console.log(response.status)
    let employeeData = response.data

    if (filter) {
      // return only employees whose name contains filter.name
      employeeData = employeeData.filter(({ employee_name }) => {
        return employee_name.toLowerCase().indexOf(filter.name.toLowerCase()) >= 0
      })
    }

    return employeeData
   } catch(error) {
     console.error(error)
     return []
   }
}

async function deleteEmployee({ id }) {
  if (!id) {
    throw new Error('You should pass a parameter')
  }
  try {
    const response = await axios({
        method: "DELETE",
        url: `http://dummy.restapiexample.com/api/v1/delete/${id}`,
        headers: { "Content-Type": "application/json" }
    })

    console.log("user deleted " + id)
  } catch(error) {
    // handle error
    console.error(error)
  }
}

async function main(params) {
   const employees = await getAllEmployees({ name: params[0] || '' })
   // Returns a promise to wait all delete promises
   return Promise.all(employess.map(employee => deleteEmployee(employee)))
}

// process.argv contains console parameters. (https://stackoverflow.com/questions/4351521/how-do-i-pass-command-line-arguments-to-a-node-js-program)
main(process.argv.slice(2)).then(() => {
  // returns 0 (Success) (https://stackoverflow.com/questions/5266152/how-to-exit-in-node-js)
  process.exit(0)
}).catch(() => {
  // returns 1 (error)
  process.exit(1)
})

您应该调整此样本以获取正确的过滤和错误报告。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM