Tag: express

将未定义的JSON投射到新的JSON对象中

未定义的JSON从URL api端点http : // someserver:someport / some_api_url?_var1=1返回到Node.js / Express.js应用程序。 这个未定义的JSONinput将始终具有相同的格式,需要通过接收代码(如下所示)将其转换为新的JSON对象。 新的JSON对象然后可以以各种方式进行处理,包括被传递到HTML。 OP问题: 需要对下面的代码做出什么特殊的改变,以便: 1.) {"1":"descriptive string"}forms的JSON被dataElement为一个新定义的名为dataElement JSON对象, dataElement包含两个属性dataElement.dataKey和dataElement.descriptionString ? 2.)发送回用户的Web浏览器的html响应包括UI格式的dataElement.dataKey和dataElement.descriptionString : Index is: 1 Description is: descriptive string 示例代码将被修改: 对于初学者来说,需要修改的代码是: app.get('/', function (req, res) { var url = 'http://someserver:someport/some_api_url?_var1=1' http.get(url, function (resInner) { var body = ''; resInner.on('data', function (chunk) { body += chunk; }); […]

Nodejs:使用用户参数进行GET请求

我是nodejs中的新手。 我正在做一个快递服务器。 我通过url收回一些参数,我想用recived url中的参数向另一个服务器发出请求。 var express = require("express"), app = express(), bodyParser = require("body-parser"), methodOverride = require("method-override"); request = require('request'); Client = require('node-rest-client').Client; app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.use(methodOverride()); var router = express.Router(); app.use(router); router.get('/get_metric_names_by_pattern', function(req, res) { console.log('get_metric_names_by_pattern '+req.url); console.log(req.query.pattern); //console.log(req.parameters); res.header('Access-Control-Allow-Origin', '*'); var client = new Client(); var salida; console.log(req.params.name); //Creamos la variable […]

我正在做这个API把请求纠正与mongoose?

所以我正在使用mongoose和mongodb,并表示路由来做到这一点。 我有一个用户文档的创build“todolist”空数组,我想能够编辑这个任务添加到列表以及编辑这些任务。 现在我正在考虑最好的办法是让服务器检查req.body是否有一些variables来设置我需要编辑的文档的哪一部分。 这是正确的方法,或者我想为这样的事情创build一个新的API路由,或者我通过检查来自客户端的reqvariables,以确定我想要在服务器上编辑正确做到这一点? 还有更干净的方法吗? router.put('/:user_id', function(req, res) { User.findById(req.params.user_id, function(err, user){ if(err){ res.send(err); return err; } if(req.body.createTask) { user.todolist.push({ "task_name": req.body.task_name, "task_importance": req.body.importance, "task_completed": false }) }else if(req.body.edit_task){ if(req.body.edit_task_name) { user.todolist[req.body.index].task_name = req.body.new_task_name; }else if(req.body.edit_task_importance) { user.todolist[req.body.index].task_importance = req.body.new_task_importance; }else if(req.body.edit_task_completed) { user.todolist[req.body.index].task_completed = true; } } user.save(function(err){ if(err) res.send(err); res.json({message: 'User updated'}); }) […]

Mongoose save()实际上并不保存

我试图find我的logging,更新它,并保存它。 我可以看到更新的数据,它仍然可以在save()的callback中显示更新的数据。 但是,当我去到数据库,它实际上没有更新: Skills.findOne({ skillsCat: req.body.skillsCat }, (err, gets)=> { if (err) { res.send(err) return } if (gets && gets.skillName.indexOf(req.body.skillName) !== -1) { // Here I update my data gets.percent[gets.skillName.indexOf(req.body.skillName)] = req.body.percent Console.log(gets); // Here I can see the data is updated return gets.save((err, updated)=> { Console.log(updated); // Here I can see the data is […]

Bookshelf.js在创build新logging之前进行独特的电子邮件validation

我试图validation电子邮件在创build新logging时已经在Bookshelf.js中存在validation。 我在这里find了一个解决schemegithub,但它不工作,即使我尝试与诺言 User = bookshelf.Model.extend({ tableName: 'users', initialize: function() { this.on('saving', this._assertEmailUnique); }, _assertEmailUnique: function(model, attributes, options) { if (this.hasChanged('email')) { return this .query('where', 'email', this.get('email')) .fetch(_.pick(options, 'transacting')) .then(function (existing) { if (!existing) throw new Error('duplicate email'); }); } } }); 对于目前正在使用Joi的 Modelvalidation,看起来Joi也不支持对此进行自定义validation。 我正在使用Postgres数据库。 有任何其他的方式来做它..请帮助… 提前致谢..

启动服务器时找不到模块“express-stormpath”

当我尝试运行node server.js (在普通的terminal,而不是一个超级用户或根),我得到这个错误: alphaunlimitedg@AUNs-PC:~/my-webapp$ node server.js module.js:442 throw err; ^ Error: Cannot find module 'express-stormpath' at Function.Module._resolveFilename (module.js:440:15) at Function.Module._load (module.js:388:25) at Module.require (module.js:468:17) at require (internal/module.js:20:19) at Object.<anonymous> (/home/alphaunlimitedg/my-webapp/server.js:2:17) at Module._compile (module.js:541:32) at Object.Module._extensions..js (module.js:550:10) at Module.load (module.js:458:32) at tryModuleLoad (module.js:417:12) at Function.Module._load (module.js:409:3) alphaunlimitedg@AUNs-PC:~/my-webapp$ 我用root,superuser和normaluser命令安装了express软件包: npm install express 它的工作,没有给任何错误! 但它仍然显示上面的错误。 任何想法如何解决这个问题? PS:这是我遵循的程序如下所示: alphaunlimitedg@AUNs-PC:~$ […]

从一个模式计算一个值,并将该值放在另一个模式mongodb节点中

var ProductSchema = new Schema({ title: {type: String}, rating : {type: Number}, ingredients : { type: String} }); var IngredientSchema = new Schema({ title: {type: String}, weightage : {type: String} }); 例如:在ProductSchema中,配料字段是:“水,锌,尿素,硅土,钠” 每种成分由IngredientSchema定义 例: title : Water | Zincum | Urea | Silica | Sodium weightage: 5 | 7 | 3 | 2 | 9 […]

是唯一的方法来给客户从服务器执行一些js代码的指令?

或换句话说: 如何发送一个客户端的消息从服务器执行一些页面上的JS代码? 这不是一个Ajax请求,它是一个普通的GET浏览器服务器请求。 比方说,我在我的应用程序的前端JavaScript文件中有这样的代码: if( condition_1 ) { FB.getLoginStatus(…) } else if( condition_2 ) { PayPal.getLoginStatus(…) } else { do smth else… } 所以我想发送那些condition_1或者condition_2指令以及服务器的响应。 这不是Ajax请求。 这是从浏览器到服务器的简单的GET请求。 目前只有cookies出现在我的脑海里。 1)服务器从浏览器收到GET请求。 2)我读了cookies。 如果没有一定的cookie伴随着请求,我设置condition_1 ,否则我设置condition_2 cookie: if(req.cookies["foo"] == "foo") { res.cookies("foo", "condition_1") .status(200) .send() .end(); } else if(req.cookies["foo"] == "bar") { res.cookies("foo", "condition_2") .status(200) .send() .end(); } 3)服务器发回一个200响应给客户端。 现在在浏览器中我检查这些cookie(为了简单起见,我做了document.cookie.foo […]

如何在nodeclipse中执行nodejs热部署,enide studio,express server?

我正在debuggingnodeclispe / enide studio中的应用程序。 我希望在express服务器运行时随时更改js文件,而不是重新启动服务器。 我怎样才能做到这一点?

通过节点JS连接到AWS MySQL数据库

我在AWS和MySQL数据库上有一个实例。 我能够通过我的本地机器上的工作台连接到实例。 但是,我无法通过节点js代码连接到数据库。 以下是摘录: var express = require("express"); var mysql = require('mysql'); var connection = mysql.createConnection({ host : 'ec2-52-33-41-xxx.us-west-2.compute.amazonaws.com', port : 3306, user : 'ec2-user', password : 'root', database : 'FAMILY_GIVING_TREE' }); var app = express(); connection.connect(function(err){ if(!err) { console.log("Database is connected … "); } else { console.log("Error connecting database … "); } }); 错误是: […]