且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Express.js在服务器上发送POST请求

更新时间:2023-02-26 14:23:01

这感觉很愚蠢,但这可能是唯一的方法?

This feels so dumb, but it might be the only way???

function postProxyMiddleware (url, data) {
  return (req, res, next) => {
    let str = []
    str.push(`<form id="theForm" action="${url}" method="POST">`)
    each(data, (value, key) => {
      str.push(`<input type="text" name="${key}" value="${value}">`)
    })
    str.push(`</form>`)
    str.push(`<script>`)
    str.push(`document.getElementById("theForm").submit()`)
    str.push(`</script>`)
    return res.send(str.join(''))
  }
}

app.get('/mock', postProxyMiddleware('/page', exampleHeaders))