在编写Web应用程序时,将(服务器端)所有日期时间存储在数据库中作为UTC时间戳是有意义的.
当我发现你无法在JavaScript中进行时区操作时,我感到非常惊讶.
我稍微扩展了Date对象.这个功能有意义吗?基本上,每次我向服务器发送任何内容时,它都将是使用此功能格式化的时间戳...
你能看到这里有什么重大问题吗?或者从不同角度解决问题?
Date.prototype.getUTCTime = function(){
return new Date(
this.getUTCFullYear(),
this.getUTCMonth(),
this.getUTCDate(),
this.getUTCHours(),
this.getUTCMinutes(),
this.getUTCSeconds()
).getTime();
}
Run Code Online (Sandbox Code Playgroud)
这对我来说似乎有点令人费解.而且我对表现也不太确定.
我试图找出实际上是什么"签名的cookie".网上没有多少,如果我试试这个:
app.use(express.cookieParser('A secret'));
Run Code Online (Sandbox Code Playgroud)
但是仍然......浏览器上的Cookie仍然100%正常,我真的不知道"签名"在这里是什么(我有点希望"看到"客户端上的一些奇怪之处,就像使用加密的数据一样作为盐的"秘密"?)
文档说(https://github.com/expressjs/cookie-parser):
解析Cookie标头并填充
req.cookies
由cookie名称键入的对象.(可选)您可以通过传递一个secret
字符串来启用已签名的cookie支持,该字符串req.secret
可以被其他中间件使用.
有人知道吗?
芝加哥商业交易所.
我正在尝试使用节点调试器.我正在node debug server
运行我的服务器.然后我有:
...
var Workspace = mongoose.model('Workspace');
debugger;
Run Code Online (Sandbox Code Playgroud)
此时,正如预期的那样,当我运行此代码时,调试器会弹出.但是,我希望它能够设置所有当前变量,就像在Chrome自己的调试器中一样.
但:
break in hotplate/node_modules/bd/lib/bd.js:133
132
133 debugger;
134
135 // Delete the ID and the version since there's no point,
debug> Workspace
ReferenceError: Workspace is not defined
Run Code Online (Sandbox Code Playgroud)
那么......我如何实际检查当前变量?
额外的问题:有没有办法使用Chrome的开发人员工具(CTRL-J),以便它连接到节点并以这种方式工作?(我知道节点检查员,但它已经过时了......)
文档内容如下:
app.VERB()方法在Express中提供路由功能,其中VERB是HTTP谓词之一,例如app.post().可以给出多个回调,所有都被平等对待,并且表现得像中间件一样,但有一个例外,即这些回调可以调用next('route')来绕过剩余的路由回调.该机制可用于在路由上执行前置条件,然后在没有理由继续匹配的路由时将控制传递给后续路由.
他们的意思是"绕过剩余的路线回调?"?我知道next()
会将控制传递给下一个匹配的路线.但是......用什么功能可以控制next('route')
...?
我用节点编写了一个简单的应用程序 这取决于快递,mongodb和猫鼬(简单).所以,我创建了一个名为的文件package.json
并将其放入其中:
{
"name": "booking-dojo",
"description": "Booking dojo app",
"version": "0.0.1",
"private": true,
"dependencies": {
"express": "3.x",
"mongodb": "*",
"mongoose": "*"
}
}
Run Code Online (Sandbox Code Playgroud)
然后我跑了npm install
,期待NPM安装这些模块和它们的依赖.结果令人失望:
booking-dojo@0.0.1 /home/merc/Synced/Development/Bookings/app/server
??? express@3.0.0rc3
? ??? commander@0.6.1
? ??? connect@2.4.3
? ? ??? bytes@0.1.0
? ? ??? formidable@1.0.11
? ? ??? pause@0.0.1
? ? ??? qs@0.4.2
? ??? cookie@0.0.4
? ??? crc@0.2.0
? ??? debug@0.7.0
? ??? fresh@0.1.0
? ??? methods@0.0.1
? ??? mkdirp@0.3.3
? ??? range-parser@0.0.4
? ??? …
Run Code Online (Sandbox Code Playgroud) 更新:我在驱动程序上使用2.1版本,而不是3.2
我有一个使用MongoDB的节点应用程序.我遇到的问题是,如果MongoDB服务器由于任何原因而关闭,则应用程序不会重新连接.为了做到这一点,我将测试基于此官方教程中的代码.
var MongoClient = require('mongodb').MongoClient
, f = require('util').format;
MongoClient.connect('mongodb://localhost:27017/test',
// Optional: uncomment if necessary
// { db: { bufferMaxEntries: 3 } },
function(err, db) {
var col = db.collection('t');
setInterval(function() {
col.insert({a:1}, function(err, r) {
console.log("insert")
console.log(err)
col.findOne({}, function(err, doc) {
console.log("findOne")
console.log(err)
});
})
}, 1000)
});
Run Code Online (Sandbox Code Playgroud)
想法是运行此脚本,然后停止mongod,然后重新启动它.那么,我们走了:
停止MongoDb 10秒会产生预期的结果:它将停止运行10秒的查询,然后在服务器返回ip后运行所有查询
在30秒之后,我开始得到:
{ [MongoError: topology was destroyed] name: 'MongoError', message: 'topology was destroyed' }
insert
{ [MongoError: topology was destroyed] name: …
Run Code Online (Sandbox Code Playgroud) 我想将数组['one', 'two', 'three', 'four']
转换为one, two, three and four
请注意,第一个项目有一个逗号,但在and
倒数第二个和最后一个之间有单词.
我提出的最佳解决方案:
a.reduce( (res, v, i) => i === a.length - 2 ? res + v + ' and ' : res + v + ( i == a.length -1? '' : ', '), '' )
Run Code Online (Sandbox Code Playgroud)
它是基于在添加逗号结束 -与第二最后一个(除外a.length - 2
),并用的方式,以避免最后一个逗号(a.length - 2
).
SURELY必须有一个更好,更整洁,更智能的方式来做到这一点?
在搜索引擎上搜索是一个很难的主题,因为它包含"和"这个词......
我正在编写一个将公开API的应用程序.该应用程序允许人们创建工作区并向其添加用户.每个用户都有一个唯一的令牌.当他们进行API调用时,他们将使用该令牌(将其标识为使用该工作空间的用户).
目前我这样做:
var w = new Workspace(); // This is a mongoose model
w.name = req.body.workspace;
w.activeFlag = true;
crypto.randomBytes(16, function(err, buf) {
if(err){
next(new g.errors.BadError503("Could not generate token") );
} else {
var token = buf.toString('hex');
// Access is the list of users who can access it. NOTE that
// the token is all they will pass when they use the API
w.access = { login: req.session.login, token:token, isOwner: true };
w.save( function(err){
if(err){
next(new g.errors.BadError503("Database error saving workspace") …
Run Code Online (Sandbox Code Playgroud) 在Web组件中,要注册元素,只需键入:
var XFoo = document.registerElement('x-foo', {
prototype: Object.create(HTMLElement.prototype)
});
Run Code Online (Sandbox Code Playgroud)
要创建元素,您可以执行以下操作之一:
<x-foo></x-foo>
var xFoo = new XFoo();
document.body.appendChild(xFoo);
var xFoo = document.createElement( 'x-foo')
document.body.appendChild(xFoo);
Run Code Online (Sandbox Code Playgroud)
这一切都很好,花花公子.当您谈论扩展现有元素时,问题就开始了.
var XFooButton = document.registerElement('x-foo-button', {
prototype: Object.create(HTMLButtonElement.prototype),
extends: 'button'
});
Run Code Online (Sandbox Code Playgroud)
问题1:为什么重复?在这里,'button'
应该足够了(特别是因为它很容易计算元素的原型Object.getPrototypeOf(document.createElement(tag));
问题2:内部如何使用该信息?例如,如果您拥有prototype: Object.create(HTMLFormElement.prototype
和extends: 'button'
(后面的内容extends
与原型传递不匹配)会发生什么
要创建一个,您可以执行以下操作之一:
<button is="x-foo-button"></button>
var xFooButton = new XFooButton();
document.body.appendChild(xFoo);
var xFooButton = document.createElement('button', 'x-foo-button');
document.body.appendChild(xFooButton);
Run Code Online (Sandbox Code Playgroud)
问题3:因为很明显x-foo-button
扩展了button
,为什么我们在使用时必须指定它们document.createElement()
?我怀疑那是因为document.createElement()
只是创建一个带语法的标签<button is="x-foo-button"></button>
,这让我想到了下一个问题:
问题4 …
如果我使用Date.parse()
,我"几乎可以保证"能够解析我程序中的内容吗?
在Mozilla的Date.parse条目中,他们写道:
给定表示时间的字符串,parse()返回时间值.它接受RFC2822/IETF日期语法(RFC2822第3.3节),例如"Mon,25 Dec 1995 13:30:00 GMT".
我自己的服务器返回Sun, 24 May 2015 05:37:13 GMT
.
麻烦的是,维基百科告诉我,Date
标题遵循RFC 7131.现在,RFC 7231虽然他们似乎在说同样的事情(虽然7231更广泛),但我想知道......
如果我使用Date.parse()
,我"几乎可以保证"能够解析我程序中的内容吗?我很高兴地认为服务器没有在时间真空中运行.