我在nodejs中有这个脚本,它工作正常 它只需要使用post命令一个带有4个字段(地址)的json并发回经度和纬度 我今天尝试了它,它不再工作而不改变任何东西
当我发送像这样的curl命令时出现错误:
curl -d '{"civico":NUMBER,"via":STREET,"citta":CITY,"stato":STATE}' -H "Content-Type: application/json" localhost:8080
错误:
SyntaxError: Unexpected token '
at parse (C:\Users\Edoardo\node_modules\body-parser\lib\types\json.js:83:15)
at C:\Users\Edoardo\node_modules\body-parser\lib\read.js:116:18
at invokeCallback (C:\Users\Edoardo\node_modules\raw-body\index.js:262:16)
at done (C:\Users\Edoardo\node_modules\raw-body\index.js:251:7)
at IncomingMessage.onEnd (C:\Users\Edoardo\node_modules\raw-body\index.js:307:7)
at emitNone (events.js:86:13)
at IncomingMessage.emit (events.js:185:7)
at endReadableNT (_stream_readable.js:974:12)
at _combinedTickCallback (internal/process/next_tick.js:74:11)
at process._tickCallback (internal/process/next_tick.js:98:9)
代码:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.json());
var APIKey = "API";
var z = "";
//POST
app.post("/", function(req, res) {
var civico = req.body.civico+'+';
var via = req.body.via.replace(/ /g,'+')+',+';
var citta = req.body.citta.replace(/ /g,'+')+',+';
var stato = req.body.stato;
var address = civico+via+citta+stato;
console.log(address);
geocode(address,res);
console.log(z);
});
function geocode(input,res){
var str = '';
var http = require('https');
var options = {
host: 'maps.googleapis.com',
path: '/maps/api/geocode/json?address='+input+'&key='+APIKey
};
callback = function(response) {
response.on('data', function (chunk) {
str += chunk;
return
});
response.on('end', function () {
//console.log(str);
//fare parsing di str e inviare i dati al client
var obj = JSON.parse(str);
var lat = obj.results[0].geometry.location.lat
var lng = obj.results[0].geometry.location.lng
str = "Posizione:\nLatitudine -> "+lat+"\nLongitudine -> "+lng+"\n"
res.status(200).send(str);
});
}
http.request(options, callback).end();
}
app.listen(8080, console.log('Server listening on port 8080...'));
问题是什么? 感谢