2017-08-01 67 views
1

我有一些設備與本地服務器上的Express應用程序進行交互。對於原型演示的目的,我試圖儘可能簡化。在許多其他的事情,我的本地服務器與圖像交互,並在/上傳圖像數據正是如此文件夾:Express(NodeJS)無法處理來自Xamarin Android應用程序的帖子

(快遞/的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"); 
     } 
    } 

服務器看到post請求,但返回500看來,它不是路由正確的,因爲它不會進入我的後處理代碼並打印一個簡單的測試字符串。有關爲什麼POST命令沒有得到適當處理的任何想法?

回答

0

嘗試用res.send()替換res.end。像這樣的東西

.post(function (req, res) { 
    Console.log("insideit"); 
    Console.log(req.body); 

    res.send('got something?'); 
    }); 

這應該有效。

+0

你好。沒有改變的錯誤。我仍然可以「POST /上傳500 1197.323毫秒 - 1591」。仍然沒有得到我的任何打印功能或任何表示發佈請求正在被這段代碼處理。 –

+0

@GregoryHewitt你能給我一個錯誤的確切文字嗎? –

相關問題