我有一些与本地服务器上的Express应用程序交互的设备。出于原型演示的目的,我正在尝试使其尽可能简单。除其他外,我的本地服务器与图像进行交互,因此,/ uploads文件夹中的图像数据也是如此:
(Express / NodeJS代码):
app.route('/uploads')
//Send File to Android
.get( function (req, res) {
var images = fs.readdirSync('./uploads');
console.log(images[images.length-1]);
var imgpath = path.resolve('./uploads/' + images[images.length-1]);
res.sendFile(imgpath);
//res.end('Image List Sent')
})
//Receive image chip data from Android
.post( function (req, res) {
Console.log("insideit");
Console.log(req.body);
res.end('got something?')
});
此服务器代码正在接收来自C#Android代码的请求。 GET命令可以完美地工作,因此我将省略Xamarin / C#代码。因此,来自Android应用程序的POST命令是(在C#/ Xamarin中):
var rxcui = "198840";
string _url = string.Format(@"http://10.1.10.194:3000/uploads", rxcui);
string datastr = "test";
try
{
(new WebClient()).UploadString(_url, datastr);
}
catch
{
Console.WriteLine("Post Upload Error");
}
}
服务器看到发帖请求,但返回500。看来它路由不正确,因为它不会进入我的发帖处理代码中并打印简单的测试字符串。关于为何未正确处理POST命令的任何想法?
最佳答案
尝试用res.send()替换res.end。像这样
.post( function (req, res) {
Console.log("insideit");
Console.log(req.body);
res.send('got something?');
});
这应该工作。