Tag: 表示

Express:为所有模板传递variables

我尝试将variables传递给所有模板。 在app.js中我写道: app.use(function(req, res, next){ res.locals.token = '1234'; next(); }); 但是,如果我试图让我的variables在模板中,如 span= token 我得到一个错误='标记未定义'。 哪里不对? 我使用Express 3.2

如何在Node.js Express应用程序中呈现预编译的Jade(Pug)模板?

我有一个nodejs应用程序,使用Express和Jade(现在帕格)。 我想预编译玉石模板以获得更好的性能。 我已经能够使用jade –client将所有.jade文件转换为相应的.js。 如何在我的应用程序中使用这些js文件? 我没有很多nodejs / express的经验,但我认为它必须要求改变渲染引擎? 我已阅读官方文档,说使用runtime.js但我找不到任何文件,如何使用。

访问socket.on('connection')闭包之外的套接字

下面是app.js的一部分,它和客户端有socket连接 io.sockets.on('connection', function (soc) { soc.emit('news', { status: 'connected' }); }); 我想要做的就是访问连接闭包之外的soc var,就像这样 io.sockets.on('connection', function (soc) { do something magical here so I can access soc from outside }); soc.emit('news', { status: 'connected' }); 需要添加什么额外的技术来存档这个结构?

快递在重新加载页面10次后停止工作

我使用快递作为我的networking服务器的节点,一切似乎正常工作。 我唯一的问题是当我重复加载一个特定的页面('/学习'路线)10次。 一旦我这样做,表示似乎停止工作,虽然没有错误logging到控制台,并没有任何错误显示在页面上。 它只是在浏览器中等待主机。 奇怪的是,如果我从带有问题的页面转到另一页面,然后再回来,问题不会发生。 我可以尽可能多地重复这一点,而不会出错。 这是我的路线与问题: var bcrypt = require('bcrypt'); var pool = require('../database.js').pool; module.exports = function(app) { app.get('/learn', function(req, res, next) { var query = 'SELECT * FROM questions INNER JOIN answers ON questions.questionID = answers.questionID'; pool.getConnection(function(err, connection) { connection.query(query, function(err, rows) { if (err) { throw err; } var data = { […]

如何replaceObjectId ref。 与来自MongoDB的实际对象(idealy在服务器端)?

这是主要对象的模式: var newsSchema = new Schema({ headline: String, paragraph: String, imgURI: String, imgThumbURI: String, imgCaption: String, addedOn: Date, addedBy: { type: ObjectID, ref: 'usr' } }); var News = mongoose.model('news', newsSchema); …和addedBy的模式: var usr = new Schema({ username: String, avatar: { type: ObjectID, ref: 'avtr' }, href: String }); var UserModel = mongoose.model('usr', usr); 到现在为止还挺好。 […]

Passport AngularJS ExpressJS:Access-Control-Allow-Origin不允许Origin null

当我试图使用angular.js的$ http模块来授权twitter应用程序时,我总是得到: XMLHttpRequest cannot load https://api.twitter.com/oauth/authenticate?oauth_token=something. Origin null is not allowed by Access-Control-Allow-Origin. 客户代码: $http({ method: 'GET', headers: { "Content-Type": undefined }, url: '/oauth/twitter' }); 服务器代码: app.configure(function () { app.use(express.cookieParser()); app.use(express.cookieSession({ secret: 'tobo!', cookie: { maxAge: 3600 }})); app.use(express.session({secret: 'secret'})); app.use(passport.initialize()); app.use(passport.session()); }); app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Credentials", true); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header("Access-Control-Allow-Headers", […]

Sails – 捕获使服务器崩溃的全局适配器错误

我试图find处理连接错误的最佳位置,或任何其他全局错误,使服务器崩溃。 捕捉适配器/全局错误,并没有让服务器崩溃的正确的地方是什么? 具体而言,我想以一种优雅的方式处理这些types的错误: Error spawning mySQL connection: error: Hook failed to load: orm (Error: connect ECONNREFUSED) error: Error encountered while loading Sails core! error: Error: connect ECONNREFUSED

在NodeJS / Express中使用POST方法从URL中获取参数

我想要做下面的事情,但我不知道如果可能的话,不要在URL中进行string操作。 我想从url中获取一些参数,我有一些数据在post数据中传递,但是我需要从URL中获取一些信息(userID): 我正在使用快递 我正在使用post方法 我的url如下所示: http://www.mydomain.com/api/user/123456/test?v=1.0 我有下面的代码,以获得所有发布请求: var http = require('http'); var url = require('url') ; exp.post('*', function(req, res, next) { var queryObject = url.parse(req.url,true).query; // not working only get in the object the value v=1.0 var parameter = req.param('name'); // get undefined } 我错过了什么? 谢谢

在节点和expression式中形成元素数组键

尽pipe我尽了最大的努力,但我一直在获取提交给NodeJS和Express以及句柄的表单元素的数组键。 我的表单元素看起来像这样: {{#each block}} <input type='text' name='block_payout[{{id}}]' /> {{/each} 这会在浏览器中产生以下标记: <input type='text' name='block_payout[14]' /> <input type='text' name='block_payout[15]' /> <input type='text' name='block_payout[16]' /> 在PHP中这将导致一个数组作为$ _POST数组的元素: $_POST [ block_payout [ 14 => value1 15 => value2 16 => value3 ] ] 但是,Node / Express中的req.body属性会删除这些键并创build一个索引数组: req.body [ block_payout [ 0 => value1 1 => value2 2 => value3 […]

错误:ENOENT,打开favicon.ico

我是节点和expression新手 我首先写了一个快速示例项目 express test1 然后我做了 npm install 为了testing应用程序,我正在做节点app.js,但我得到这个错误 错误:ENOENT,打开'/home/test1/node_modules/express/node_modules/connect/node_modules/static-favicon/../public/favicon.ico' 我做了一些谷歌search,我不知道解决scheme,这是从明确的演示应用程序,应该工作,我已经厌倦了这个星期,这是工作。 任何见解,将不胜感激