我正在为我的课程设计一个项目。该应用程序可帮助用户升级其PC。该应用程序包含三个部分,它们是响应前端,node.js服务器和一个C#应用程序(扫描仪),用户可以从其下载并在用户PC上本地运行它。扫描程序运行后,它将从用户的PC中提取现有的硬件规格,并将其作为json对象通过HTTP请求发送到服务器。服务器API会获取此信息,并从我们的数据库生成兼容的升级零件清单。我正在努力寻找一种方法来将此API生成的数据返回给在用户端打开的react前端。
API是REST API
我不熟悉使用节点js和处理HTTP请求。如果有人可以提供任何有助于实现此功能的解决方案,文档或文章,将对您有所帮助。也可以就如何实现这三个部分之间的这种联系提出任何建议。
这是响应前端发送对scan.exe的请求的代码
handleDownload = () =>{
const scn_URL = "http://localhost:5000/fileApi?name=User_scanner.exe";
Axios({
method: 'GET',
url: scn_URL,
responseType: 'blob'
})
.then(response =>{
console.log(response.data);
const url = window.URL.createObjectURL(new Blob([response.data]));
const link = document.createElement('a');
link.href = url;
link.setAttribute('download', 'scanner.exe');
document.body.appendChild(link);
link.click();
})
}
这是服务于scan.exe文件的服务器端API代码
var express = require("express");
var router = express.Router();
router.get("/", function (req, res, next) {
const fileName = req.query.name;
const filepath = "C:\\Users\\Sakib_PC\\my files\\senior_Design\\Senior_DesignS19_group_5\\back-end-testing\\serverAPI\\public\\User_scanner.exe"
console.log(`Got file name: ${fileName}`);
res.set({'Content-Type':'application/octet-stream'})
res.sendFile(filepath, function (err) {
if (err) {
next(err)
} else {
console.log('Sent:', fileName)
}
});
});
module.exports = router;
扫描程序将信息作为json发送到服务器,并在下面的代码中将其命名为“ data”
private async void SendJson_Click(object sender, EventArgs e)
{
Console.WriteLine("send button was clicked");
const string url = "http://localhost:5000/testApi";
var data = new StringContent(this.jsonData, Encoding.UTF8, "application/json");
string response;
HttpClient client = new HttpClient();
HttpResponseMessage res = await client.PostAsync(url, data);
response = res.Content.ReadAsStringAsync().Result;
Console.WriteLine(response);
if (response == "message recieved")
{
Console.WriteLine(response);
this.finishButton.Enabled = true;
}
else
{
Console.WriteLine("nothing ");
}
}
捕获此数据的API的代码如下:
var express = require("express");
var router = express.Router();
router.post("/", function (req, res, next) {
const msg = req.body;
console.log(msg);
const modMsg = "message recieved";
res.send( modMsg);
})
module.exports = router;
服务器从扫描仪接收的数据在预期的链接中给出。 image of the json data being send by the scanner 有什么方法可以将捕获到的api的扫描程序数据发送到第一手做出下载请求的前端。请记住,从网站发出的下载GET请求和从扫描程序发出的POST请求是两个不同的请求。我的目标是从扫描仪POST请求中获取数据,并从发出下载请求的位置将其提供给客户端
谢谢!