我应该如何使用Node.js解析JSON ?是否有一些模块可以安全地验证和解析JSON ?
当前回答
JSON。解析将不能确保你正在解析的json字符串的安全性。您应该查看类似于json-safe-parse或类似的库。
来自json-safe-parse npm页面:
JSON。parse非常棒,但是它在JavaScript上下文中有一个严重的缺陷:它允许重写继承的属性。如果解析JSON来自一个不受信任的源(例如:用户),并在其上调用您希望存在的函数,这就会成为一个问题。
其他回答
解析JSON流?使用JSONStream。
var request = require('request')
, JSONStream = require('JSONStream')
request({url: 'http://isaacs.couchone.com/registry/_all_docs'})
.pipe(JSONStream.parse('rows.*'))
.pipe(es.mapSync(function (data) {
return data
}))
https://github.com/dominictarr/JSONStream
您可以简单地使用JSON.parse。
JSON对象的定义是ECMAScript 5规范的一部分。node.js基于谷歌Chrome V8引擎,符合ECMA标准。因此,node.js也有一个全局对象JSON[docs]。
注- JSON。Parse会占用当前线程,因为它是一个同步方法。因此,如果您计划解析大型JSON对象,请使用流式JSON解析器。
为了安全起见,用这个吧
var data = JSON.parse(Buffer.concat(arr).toString());
使用JSON对象:
JSON.parse(str);
使用JSON配置Node.js?阅读这篇文章,让你的配置技能超过9000…
Note: People claiming that data = require('./data.json'); is a security risk and downvoting people's answers with zealous zeal: You're exactly and completely wrong. Try placing non-JSON in that file... Node will give you an error, exactly like it would if you did the same thing with the much slower and harder to code manual file read and then subsequent JSON.parse(). Please stop spreading misinformation; you're hurting the world, not helping. Node was designed to allow this; it is not a security risk!
正确的应用程序有3层以上的配置:
服务器/容器配置 应用程序配置 (可选)租户/社区/组织配置 用户配置
大多数开发者都认为他们的服务器和应用配置是可以改变的。它不能。您可以在更高的层上逐层进行更改,但是您正在修改基本需求。有些东西需要存在!让你的配置像不可变一样,因为它的一些基本是不可变的,就像你的源代码一样。
如果没有看到你的很多东西在启动后不会改变,就会导致反模式,比如在配置加载中到处都是try/catch块,并且假装你可以在没有正确设置应用程序的情况下继续运行。你不能。如果可以,那就属于社区/用户配置层,而不是服务器/应用配置层。你只是做错了。当应用程序完成引导时,可选的东西应该分层在顶部。
不要用头撞墙:你的配置应该非常简单。
看看用一个简单的json配置文件和一个简单的app.js文件来设置一个像协议无关和数据源无关的服务框架这样复杂的东西是多么容易……
container-config.js……
{
"service": {
"type" : "http",
"name" : "login",
"port" : 8085
},
"data": {
"type" : "mysql",
"host" : "localhost",
"user" : "notRoot",
"pass" : "oober1337",
"name" : "connect"
}
}
index.js……(驱动一切的引擎)
var config = require('./container-config.json'); // Get our service configuration.
var data = require(config.data.type); // Load our data source plugin ('npm install mysql' for mysql).
var service = require(config.service.type); // Load our service plugin ('http' is built-in to node).
var processor = require('./app.js'); // Load our processor (the code you write).
var connection = data.createConnection({ host: config.data.host, user: config.data.user, password: config.data.pass, database: config.data.name });
var server = service.createServer(processor);
connection.connect();
server.listen(config.service.port, function() { console.log("%s service listening on port %s", config.service.type, config.service.port); });
app.js……(支持协议不可知和数据源不可知服务的代码)
module.exports = function(request, response){
response.end('Responding to: ' + request.url);
}
使用这种模式,你现在可以在你启动的应用程序上加载社区和用户配置的东西,开发运维已经准备好将你的工作推到一个容器中并扩展它。你是为多租户的。用户域是隔离的。现在,您可以分离使用的服务协议和数据库类型,只关注编写好的代码。
因为你在使用层,你可以在任何时候依赖于一个单一的真实来源(分层配置对象),并避免在每一步进行错误检查,担心“哦,糟糕,没有正确的配置,我怎么能让这个工作?!?”
推荐文章
- 使用Python将XML转换为JSON ?
- 当你双击日文文本时,Chrome如何决定突出显示什么?
- npm install和npm run build有什么区别?
- 使用async/await尝试/捕获块
- 为什么.json()返回一个承诺?
- 更改mocha的默认超时时间
- 如何设置一个iframe src属性从一个变量在AngularJS
- 解析日期没有时区javascript
- 是否有一种方法可以检查数据属性是否存在?
- Chrome不会删除会话cookie
- 如何在JavaScript中获得UTC时间戳?
- 如何使用jQuery等待CSS3过渡结束?
- 在Chrome和Firefox中用Javascript报告的神秘的“脚本错误”
- 设置CSS属性在JavaScript?
- 右击是Javascript事件吗?