2017-05-07 28 views
3

我的REST服務如下:REST的NodeJS土耳其字符,插座掛斷

const 
 
    express = require('express'), 
 
    app = express(); 
 
    
 
    ... 
 
    
 
app.get('/turkish-escape/:id', function(req, res) { 
 
    console.log("converting turkish special characters:"); 
 
    console.log("\u011f \u011e \u0131 \u0130 \u00f6 \u00d6 \u00fc \u00dc \u015f \u015e \u00e7 \u00c7"); 
 
    let char = req.params.id; 
 
    
 
.....//DO stuff

當我試圖讓在瀏覽器的服務,我沒有得到任何錯誤: http://localhost:8081/turkish-escape/ğ/

當我嘗試用我的REST客戶端獲得結果時,我遇到了一些土耳其特殊字符的問題。

客戶端代碼:

let currentChar = text[i]; 
 
    let 
 
    request = require('request'), 
 
    options = { 
 
     method: 'GET', 
 
     url: 'http://localhost:8081/turkish-escape/' + currentChar + "/" 
 
    }; 
 
    request(options, function(err, res, body) { 
 
     if(err) { 
 
     throw Error(err); 
 
     } else { 
 
     body = JSON.parse(body); 
 
     console.log(body.convertedChar); 
 
     newText += body.convertedChar; 
 
     } 
 
    });

當我打電話與「U」它工作正常,但如果我用「G」叫它它崩潰的客戶端。這是呼叫和堆棧跟蹤:

./turkishTextConverter.js ğ 
/pathtofile/turkishTextConverter.js:25 
    throw Error(err); 
    ^

Error: Error: socket hang up 
at Request._callback (/pathtofile/turkishTextConverter.js:25:15) 
at self.callback (/pathtofile/node_modules/request/request.js:188:22) 
at emitOne (events.js:96:13) 
at Request.emit (events.js:191:7) 
at Request.onRequestError (/pathtofile/node_modules/request/request.js:884:8) 
at emitOne (events.js:96:13) 
at ClientRequest.emit (events.js:191:7) 
at Socket.socketOnEnd (_http_client.js:394:9) 
at emitNone (events.js:91:20) 
at Socket.emit (events.js:188:7) 

當你從它從來沒有達到甚至REST服務

+0

堆棧跟蹤似乎來自客戶端,而不是服務器。 – robertklep

+0

謝謝,但爲什麼我的客戶端崩潰?當我使用瀏覽器進行GET時,沒有任何問題。我需要某種字符en /decodingß – nufuk

+0

它可能是服務器崩潰,導致客戶端崩潰。無論如何,你應該使用UTF-8編碼。 – robertklep

回答

1

該解決方案的第一的console.log語句堆棧看到是是encodeURI的網址:

... 
url: 'http://localhost:8081/turkish-escape/' + currentChar + "/" 
... 

... 
url: encodeURI('http://localhost:8081/turkish-escape/' + currentChar + '/') 
... 

現在的客戶會不會崩潰了