如何从内部Office.js代码连接服务器



我正在开发一个Office.js应用程序。我还开发了一个运行并等待HTTP请求的Express服务器。但是我对如何连接两部分有麻烦(两个代码都用节点thecnology编写(我尝试了HTTP模块,以便将Office.js应用程序的发布请求发送到其他代码中的Express Server,但它不起作用。这是Office.js应用程序的代码:

const data = JSON.stringify({
    userid : 8888,
    username : 'excell sending info - third test'
  })

const options = {
  hostname: 'localhost',
  port: 8888,
  path: '/test',
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Content-Length': data.length
  }
}
const req = http.request(options, (res) => {
  console.log(`statusCode: ${res.statusCode}`)
  res.on('data', (d) => {
    process.stdout.write(d)
  })
})
req.on('badRequest', (error) => {
  console.error(error)
})
//OfficeHelpers.UI.notify();
req.write(data)
req.end()


    });
} catch (error) {
    OfficeHelpers.UI.notify(error);
    OfficeHelpers.Utilities.log(error);
}

}

Wireshark表示有不好的请求。非常感谢您的帮助!

您正在使用office.js构建办公室加载项真是太好了。您在本地运行的Express Server是在HTTPS上运行的吗?如您所知,作为一种安全措施,只能使用HTTPS加载加载项,然后连接到HTTPS的站点/服务。您可以从我们拥有的最新模板中看到,我们还创建了一个简单的WebPack-dev-server,它使用HTTP和自动生成的dev-cert。查看模板以获取有关如何生成证书并为其配置的示例服务器。https://github.com/officedev/office-addin-taskpane

就像在Express.js上为您提供快速,随机的指针一样,我发现此博客很有帮助:https://timonweb.com/posts/running-expressjs-server-over-over-https/我希望这可以帮助你!

通过使用HTTPS和创建所需的证书来解决问题。然后可以正确发送请求。

最新更新