Tag: json

用模拟函数replacenodejs模块中的函数

我正在创build一个包含API的nodejs中的小项目。 我正在使用nodeunit编写一些unit testing,并且需要向模块中注入各种模拟函数(例如模拟向服务器发出HTTP请求并输出各种不同响应以testing我的代码的函数)。 我的问题是如何将这些function注入到我的模块? 我已经确定了两个理论上应该如下工作的方法: 方法1 重命名我想要replace的模块的文件夹,并添加一个包含我想要注入的代码的新文件夹,例如: ./node_modules/request -> ./node_modules/request.tmp ./tests/myRandomFunction -> ./node_modules/request 进行testing后,我会做相反的事情: ./node_modules/request -> ./tests/myRandomFunction ./node_modules/request.tmp -> ./node_modules/request 这看起来很诡异,不是我想尝试的东西,即使理论上它应该工作。 方法2 这是我使用模块初始化的首选方法。 我的模块需要一个JSON对象,可以包含各种选项,如下所示: var module = require('./module')({ option1: '', option2: '', … }); 我打算为这个名为'_testing'的JSON对象添加一个键,其中包含各种函数的JSON对象的值,例如: var module = require('./module')({ _testing: { request: function() {return false;} } }); 在我的模块中,我可以做到以下几点: 如果this._testing存在并且是一个JSON对象 循环this._testing 对于这个._testing中的每个键 用与其值相同的名称replace该函数的值,例如 eval(''+key+' = this._testing.'+key) […]

使用JSON Web令牌和node.js / express保护URL的正确方法

我目前使用node.js授权用户使用JSON Web令牌,并使用EJS作为视图引擎进行expression。 在我的server.js文件中使用简单的中间件: app.use(function(request, response, next){ var token = request.body.token || request.query.token || request.headers['x-access-token']; console.log(request.body); if(token){ jwt.verify(token, app.get('superSecret'), function(err, decoded){ if(err){ response.json({"message": "Failed to authenticate user"}); } else{ request.decoded = decoded; next(); } }); } else{ return response.status(403).json({"message":"No token was provided"}); } }); 并保护下面的路线,例如: app.post('/userlist', function(request, response) { response.json({some: json}) }); 我无法理解或弄清楚如何保护GET路线,如: app.get('/userprofile', function(request, response) […]

NodeJS POST请求通过JSON-RPC

我试图通过JSON-RPC在我的NodeJS服务器上执行POST请求。 转换下面的curl命令: curl -X POST –data '{"jsonrpc":"2.0","method":"personal_newAccount","params":["pass"],"id":74}' http://localhost:8545 在NodeJS中,我一直在收到: 200 {"id":-1,"jsonrpc":"2.0","error":{"code":-32600,"message":"Could not decode request"}} 在标题中,我指定了Content-Type。 如果有人能指出我没有指定什么,以及如何添加它,将不胜感激。 var headers = { 'User-Agent': 'Super Agent/0.0.1', 'Content-Type': 'application/json-rpc', 'Accept':'application/json-rpc' } var options = { url: "http://localhost:8545", method: 'POST', headers: headers, form: {"jsonrpc":"2.0","method":"personal_newAccount","params":["pass"],"id":1} } request(options, function (error, response, body) { if (!error && response.statusCode == 200) { res.writeHeader(200, {"Content-Type": […]

如何在JavaScript中创builddynamic数组?

我有一个数组像 { "Count": 3, "Items": [ { "prod_price": { "N": "100" }, "prod_name": { "S": "Laptop" }, "prod_Id": { "N": "3" } }, { "prod_price": { "N": "1000" }, "prod_name": { "S": "Mouse" }, "prod_Id": { "N": "2" } }, { "prod_price": { "N": "2000" }, "prod_name": { "S": "Keyboard" }, "prod_Id": { "N": "1" […]

当事件创buildOK时,Keen.io JS api响应{“code”:“UnknownError”}

我试图从parse.com云代码(node.js)创build一个Keen事件。 我正在使用一个JS模块( https://github.com/roycef/keen-parse )似乎设置好。 为了testing,我build立了一个简单的testing,下面是完整的main.js(证书已删除): var express = require('express'); var app = express(); // Global app configuration section app.use(express.bodyParser()); var Keen = require('cloud/keen.js'); var keen = Keen.configure({ projectId: "xxxxxxxx", writeKey: "xxxxxxxx" }); app.get('/kiss', function (req, res) { var resp = {}; var respCode = 404; var testObj = {"firstname": "John", "surname": "Doe"}; // send single […]

纯文本到JSON

我的应用程序按需启动AWS ElasticBeanstalk环境。 这些EB环境自动订阅AWS SNS主题,通过HTTP POST将消息发送到我的应用程序webhook URL。 问题是,下面是一个“消息”对象的例子,数据以纯文本的forms发送到webhook,所以在消息的任何地方都有\n实例,这对我来说是没有用的。 我希望在那里有一个新的对象,我的应用程序可以清楚地访问(通过Message.Timestamp,Message.Message等) Message: 'Timestamp: Fri Aug 21 22:25:23 UTC 2015\nMessage: Adding instance 'xxx' to your environment.\n\nEnvironment: xxx\nApplication: xxx\n\nEnvironment URL: xxx\nNotificationProcessId: xxx' 那可能吗…?

stream浪汉(Saltstack)如何安装package.json与盐状态?

我想用stream浪汉“stream浪”来安装我的环境。 我做了代码来安装模块,但我想安装package.json文件,以及如何? 我的模块安装程序代码如下所示: mongodb: pkg: – installed service: – running nodejs: pkg: – installed npm: pkg: – installed git: pkg: – installed build-essential: pkg: – installed bower: npm: – installed nodemon: npm: – installed

为什么不工作“永远永远停止/ development.json”?

我使用follow命令永久安装npm包 sudo npm install forever -g npm install forever-monitor 我的server.js文件使用follow命令运行 forever start server.js 结果: warn: –minUptime not set. Defaulting to: 1000ms warn: –spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms info: Forever processing file: server.js 现在server.js运行后台 接下来的server.js文件停止使用follow命令 forever start server.js 结果: info: Forever stopped process: uid […]

Node.JS正文parsing器问题

我试图在Node.js中将数据从一台机器发送到另一台机器。 我似乎有一些困难让parsing器正常工作。 这是我的客户端和服务器代码 Client.JS var request = require('request'); request.post( 'http://192.168.1.225:3002', { form: { key: 'notyourmother' } }, function (error, response, body) { if (!error && response.statusCode == 200) { console.log(body) } } ); Server.JS var express = require('express'); var bodyParser = require('body-parser') var app = express(); app.use(bodyParser.json()); app.post('/', function (req, res) { res.send('POST request to […]

使用grunt进行自定义项目

我正在使用Grunt作为“JS Task Runner”。 节点安装在“C:/ Program Files”中。 NPM安装在C:/ users / Peterson / appdata / roaming / npm中。 grunt,bower和grunt-cli在npm文件夹里。 创build一个项目 – D:/项目A. D:/ ProjectA – “package.json”和gruntfile.js文件 的package.json { "name": "Test-Project", "version": "0.1.0", "devDependencies": { "grunt": "~0.4.1", "grunt-contrib-concat": "~0.1.3" } } gruntfile.js module.exports = function(grunt) { // Project configuration. grunt.initConfig({ //Read the package.json (optional) pkg: grunt.file.readJSON('package.json'), // Metadata. […]