node.js - 使用google API之前需要對input 做什麼 安全性的處理?

浏览:32日期:2022-09-23

问题描述

我正要使用node.js 和 google map api做一个小应用,Google MAP API的使用URL如下:

https://maps.googleapis.com/maps/api/place/textsearch/json?query=KEY+WORDS&key=YOUR_API_KEY

KEY WORDS的部分将是我在我的应用上id='keywords'这个input的value

HTML CODE:

<form action='/api/googlemap' method='POST'> <input diname='keywords' type='text'></input></form>

传到/api/googlemap后我使用node.js 的 request当作我的http client去跟google api要资料

var request = require(’request’);router.get(’/api/googlemap’,function (req, res) { var keywords = req.body.keywords; var url = ’https://maps.googleapis.com/maps/api/place/textsearch/json?query=’+keywords+’&key=myapikey’; request(’url’, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) // Show the HTML for the Google homepage. } else{ res.json(response);}}) });

请问我这样对keywords的处理是不是不够安全? 在node.js 上我应该对 req.body.keywords 做什麽样的处理之后再跟google api再使用request和API要资料?

我注意到value在URL上,google给的example对 (空格)都是使用+替代,而如果我在chrome浏览器上,URL输入空白后会自动换成 %20,那我在node.js需要使用replace(' ','+') 将 (空格)换成+吗?

谢谢

问题解答

回答1:

我在使用中文当作keywords时,会出现错误,但是用了encodeURIComponent() 把req.body.keywords之后,就正常了

相关文章: