Tag: 表示

未使用callback时未定义的结果。 Nodejs,Express和SQL Server Express

以下问题由mssql,Nodejs,Gulp,Express和SQL Server Express组成。 我能够成功login到SQL Server Express。 但是,当我使用bookRoute.js代码片段而没有callback时,返回的值是未定义的 。 然而,当我使用callback,我得到的数据。 不过,我不明白为什么。 app.js代码片段: var config = { user: 'user', password: 'password', server: 'localhost', database: 'Books', options: { instance: 'SQLEXPRESS' } }; sql.connect(config, function(err){ console.log(err); }); 没有callback的bookRoute.js代码片段: bookRouter.route('/') .get(function (req, res) { console.log('book router'); var request = new sql.Request(); request.query('select * from books').then( function (err, recordset) { console.log(recordset); […]

express.js的文档

像Java一样有http://www.docjar.com/查看java中所有类的源代码,有没有任何资源可以查看express模块​​的源代码。

如何使用MEAN堆栈在注册时显示失败消息

我正在开发一个使用护照authentication进行login和注册过程的SPA。 事情是,一切工作正常,我甚至可以显示成功通知,但我不能find一种方式来显示错误通知,以防用户做出不好的操作。 这是我在passport-init注册的代码: passport.use('signup', new LocalStrategy({ passReqToCallback : true }, function(req, username, password, done){ var email = req.body.email; User.findOne({$or: [ {username: username}, {email: email} ] }, function(err, user){ if(err){ return done(err, false); } if(user) { console.log('username or email already taken '+req.body.username); return done(null, false, {message: 'Username or email already taken.'}); } user = new User(); […]

如何解决pg-promise错误“必须指定promise库”。

我在ExpressJS中使用pg-promise创build了一个基本的API来与我的PostgreSQL数据库进行交互。 在Windows上运行时,它工作正常。 然后我把它移动到Ubuntu 15.04,但是当我尝试启动它时会得到以下错误: /node_modules/pg-promise/lib/promise.js:46 抛出新的TypeError(“必须指定Promise库”);

expression4:redirect后,我无法得到我的静态文件

我遇到了Express 4和静态文件的麻烦。 成功login后,我的networking应用程序redirect到“/ home / userId”,但是,所有静态文件都得到了404。 这是我的路由器: router.get('/home/:userid', function(req, res, next) { // TODO check if token is valid User.findById(req.params.userid).exec(function(err, find) { if (err) return next(err); if (!find) { return res.json(utils.createErrorJsonResponse(404, "User not found!")); } return res.render('home', { title: 'Organizator', user: find }); }) }); 我觉得是不是有用的显示你我的玉文件,唯一重要的是,有很多导入的脚本,但只是举一个例子,这是我如何加载一个CSS文件: link(href='css/style.css', rel='stylesheet') 这是我如何设置静态文件 app.use(express.static(config.root + '/public',{ maxAge: 86400000 })); […]

如何为具有参数和关系的方法编写远程钩子

我通过远程钩子文档 ,我可以成功创build远程钩子的方法没有额外的参数,如login ,这是: customer.afterRemote('login', function(ctx, modelInstance, next) { if (ctx.result) { … next(); } else{ next(); } }); 现在,如何写一个方法的远程钩子说: GET /customers/{id} POST /customers/{id} 或者在发布相关对象时 POST /customers/{id}/contacts GET /customers/{id}/contacts 我知道使用POST /customers/{id}/contacts : customer.beforeRemote('**', function(ctx, user, next) { console.log(ctx.methodString, 'was invoked remotely'); // customers.prototype.save was invoked remotely next(); }); 会返回所谓的方法的名称,如: customer.prototype .__ create__contacts被远程调用 但是我仍然无法把它具体挂钩,而下面的尝试是徒劳的,钩子没有达到: customer.beforeRemote('customer.prototype.__create__contacts', function(ctx, user, […]

Express js多个操作gm(GraphicsMagick)模块

与gm(GraphicsMagick)模块一起使用Expressjs。 当前(见代码)操作#1和#2在单独执行时正常工作,但它们不能一起工作(如下所示)。 我想在一个声明中结合这两个操作,有什么build议吗? var express = require('express'); var router = express.Router(); var gm = require('gm'); // GraphicsMagick router.get('/', function(req, res) { gm('image.png') // Operation #1 .composite('topimage.png') .geometry('+200+200') // Operation #2 .drawText(5, 20, 'my text') .fontSize(20) .font(__dirname + 'fonts/MyFont.TTF') .stream(function streamOut (err, stdout, stderr) { stdout.pipe(res); //pipe to response }); }); module.exports = router;

NodeJS是asynchronous的,我的代码不按我期望的顺序运行

postRegistrationHandler: function (account, req, res, next) { console.log('postRegistrationHandler activated'); account.getCustomData(function(err, data) { if (err) { console.log(err.toString, "error string"); return next(err); } else { data.mongo_id = userCreationCtrl(account); data.save(); next(); } }); }, 这个function几乎可以正常工作,但行: data.save(); 在前一行完成之前运行,这意味着我想要保存的数据在适当的时候不存在。 data.mongo_id = userCreationCtrl(account); 这一行调用一个函数,它创build一个包含帐户对象中的信息的mongoDB文档,然后返回_id(这就是我正在保存的内容。 我想也许使用一个.then()会有所帮助,但由于某种原因,这似乎是不可用的。 如果有人看到我错过的东西,那将是相当有帮助的。 谢谢! 这里是请求的userCreationCtrl文件: var UserSchema = require('./../models/UserModel.js'); var createNewUser = function (account, res, next){ // We […]

ExpressJS>路由器中间件>通过redirect传递variablesreq.foo不工作

比方说,我有这个: a)每条路由具有1个中间件函数和一个callback函数: router.post('/foo', fooCtrl, function(req, res) { res.redirect('/bar'); }); router.get('/bar', barCtrl, function(req, res) { res.end('finished'); }); b)2个中间件函数expression式 fooCtrl = function(req, res, next) { req.foo = 'foo'; next(); }; barCtrl = function(req, res, next) { console.log(req.foo); // output is 'undefined' next(); }; 正如你所看到的,在fooCtrl中我设置了一个variablesreq.foo – 在这个范围内它被设置为…但是我不能在barCtrl范围内调用这个variables。 是不是通过引用传递的req对象? 任何build议或最佳做法? 这样做的目的是从login控制器login后,将令牌传递给pipe理员控制器..类似的东西。

将数组属性添加到此Express.js模型类

为了在Express.js应用程序中成功地将一个string数组的属性添加到JavaScript模型对象中,需要对下面的代码进行哪些特定的更改? AppUser代码被放置在这个GitHub链接的/app/models目录下的一个新的appuser.js文件中。 这里是AppUser类的代码,包括数组的获取者和设置者的占位符,OP要求如何写: var method = AppUser.prototype; function AppUser(name) { this._name = name; } method.getName = function() { return this._name; }; //scopes method.getScopes = function() { //return the array of scope string values }; method.setScopes = function(scopes) { //set the new scopes array to be the scopes array for the AppUser instance //if the AppUser […]