我想以异步方式初始化模块并提出一些想法.我需要带有来自Mongo和其他数据的集合列表的DB对象,但是./为了简洁起见,文件列表也是如此.
我无法导出函数或类,因为我需要require('db')每次都返回相同的对象.
首先也是最简单的东西来到我的脑海里是分配module.exports给Object后来填充它:
var exports = {};
module.exports = exports;
require('fs').readdir('.', function(err, files) {
exports.error = err;
exports.files = files;
});
Run Code Online (Sandbox Code Playgroud)
不好的事情 - 当列表准备就绪并且没有检查错误的好方法时,我真的不知道从外面.
我搞砸的第二种方法是继承EventEmitter并通知每个人DB已准备好或发生错误.如果一切正常 - 继续.
var events = require('events');
var util = require('util');
function Db() {
events.EventEmitter.call(this);
this.ready = false;
this.files = null;
this.initialize();
}
util.inherits(Db, events.EventEmitter);
Db.prototype.initialize = function() {
if (this.ready)
return this.emit('ready');
var self = this;
require('fs').readdir('.', function(err, files) {
if (err) …Run Code Online (Sandbox Code Playgroud) 我一直在使用node-mongoskin连接这两个.一切都很好,直到我查询了一些"日期"字段,我认为该字段应该作为javascript的Date对象返回.但结果的类型是字符串,这是奇怪的(对我而言)并且不方便.
插入看起来像这样:
var doc = {
date: new Date(),
info: 'Some info'
}
db.users.insert( doc, {safe: true}, function(err, res) {
...
});
Run Code Online (Sandbox Code Playgroud)
以上结果是(没有_id字段):
{ "date" : "Mon Oct 24 2011 18:00:57 GMT+0400 (MSK)", "info": "Some info" }
Run Code Online (Sandbox Code Playgroud)
但是,使用MongoDB Shell插入工作正常,除了字段类型 ISODate
> db.things.insert({ date: new Date() }); db.things.find();
{ "_id" : ObjectId("4eae9f2a34067b92db8deb40"), "date" : ISODate("2011-10-31T13:14:18.947Z") }
Run Code Online (Sandbox Code Playgroud)
所以,问题是:我应该如何插入文档来查询日期字段作为Date对象?我想要的是在数据库服务器端设置字段.我只是发送类似null-fields的东西,db-server使用默认的mongo机制为我设置这些.
插入时间戳(作为本机MongoDB时间戳)也是一个问题,但它并不是什么大问题.
PS:没有运气通过mongoskin和mongodb-native docs.
我还没有考虑将文件,如Word,Excel等严格存储到MongoDB中,我想知道 - 我能在MongoDB中存储整个docx或excel文件,然后通过查询来检索它们吗?
我在Node.js + Express 2中编写代理.代理应该:
加密相关部分工作正常.我面临的问题是超时.代理应该在不到15秒的时间内处理请求.实际上,他们中的大多数都在500毫秒以下.
当我增加并行请求数时出现问题.大多数请求都已完成,但有些请求在15秒+几毫秒后失败.ab -n5000 -c300工作正常,但并发度为500时,某些超时请求失败.
我只能推测,但似乎问题是回调exectuion的顺序.首先出现的请求是否可能会挂起,直到ETIMEDOUT节点将焦点放在仍在500ms内及时处理的最新节点中.
PS:远程服务器没有问题.我正在使用请求与它进行交互.
UPD
事物适用于某些代码的方式:
function queryRemote(req, res) {
var options = {}; // built based on req object (URI, body, authorization, etc.)
request(options, function(err, httpResponse, body) {
return err ? send500(req, res)
: res.end(encrypt(body));
});
}
app.use(myBodyParser); // reads hex string in payload
// and calls next() on 'end' event
app.post('/', [checkHeaders, // check Content-Type and Authorization headers
authUser, // query DB and call …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用QtWebKit模块将照片上传到vk.com.我面临的问题是无法正确填补价值.这是我使用的一些相关代码:input(type="file")
def upload():
print 'uploading...'
photoInput = web.page().mainFrame().documentElement().findFirst('input[id="photos_upload_input"]')
assert photoInput, 'No input found'
photoInput.setAttribute('value', '/Users/elmigranto/Downloads/stuff.png')
print photoInput.evaluateJavaScript('return this.value;').toString()
Run Code Online (Sandbox Code Playgroud)
值得注意的是,由于浏览器安全策略,Javascript无法填写文件输入值.但是,应该可以使用Qt API,更具体地说,使用方法.这就是我所做的......没有效果(好吧,返回预期结果,但返回空字符串,输入的处理程序也不会被触发).QWebElement::setAttribute()photoInput.attribute('value')photoInput.evaluateJavaScript('return this.value;').toString()onchange
设置其他属性没有问题,例如,像魅力一样工作.QWebElement::addClass()
任何帮助都会非常棒.
谢谢.
我正在使用Jade作为我的Express动力node.js应用程序的模板引擎.也许是深夜,但我无法弄清楚style在结果HTML中插入空元素的位置.
实例:http://quotation-mark.herokuapp.com/(注意到<style type="text/css"></style>最后head?)
app.set('views', settings.projectDir + '/views');
app.set('view engine', 'jade');
app.set('view options', {layout: false});
Run Code Online (Sandbox Code Playgroud)
!!!html
include /helpers/css.jade
html(lang='ru')
head
meta(http-equiv='content-type', content='text/html; charset=utf-8')
noscript
meta(http-equiv="refresh", content="0; url=/noscript")
title= title
block css
mixin css('/css/bootstrap/bootstrap.css')
body
block content
Run Code Online (Sandbox Code Playgroud)
extends layout
append content
h1= title
p ????? ?????????? ? #{title}.
Run Code Online (Sandbox Code Playgroud)
即使head模板本身没有,浏览器中也有一个(Chrome 19.0.1084.53),也许浏览器会添加一些东西?简单的HTML页面没有它:
<!DOCTYPE html>
<html>
<head>
<title>Test</title>
<style>h1{color:red;}</style>
</head>
<body>
<h1>Hi!</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) node.js ×5
mongodb ×2
asynchronous ×1
express ×1
http ×1
javascript ×1
mongoose ×1
proxy ×1
pug ×1
qt ×1
qt4 ×1
qtwebkit ×1
qwebelement ×1