我有一个Backbone View,它向服务器发送Ajax调用以删除会话.
在服务器上跟随事件被触发:
app.delete('/session', function(req, res) {
if (req.session) {
req.session.destroy(function() {
res.clearCookie('connect.sid', { path: '/' });
res.send('removed session', 200);
});
} else {
res.send('no session assigned', 500);
}
});
Run Code Online (Sandbox Code Playgroud)
奇怪的是,我可以多次按下注销按钮而不会收到HTTP 500错误代码.铬也告诉我饼干仍然存在.
出了什么问题?
问候
编辑:
我发现这不是一个会话问题,而是一个cookie问题.我在路线上添加了res.clearCookie.不幸的是,行为(cookie,会话保持活跃)没有改变
EDIT2:我现在给res.clearCookie一些参数=> res.clearCookie('connect.sid',{path:'/'}); 现在至少cookie已经在浏览器中消失了.但会议似乎仍然可用.或者至少我可以调用注销路径我甚至需要req.session应该是假的
编辑3: 我现在从redis中删除了所有会话并重新启动了所有内容(redis,node,browser).比我再次登录并退出.这工作到目前为止,但当我用F5重新登录页面时,我得到一个新的会话.为什么?
我有以下页面布局:
<header></header>
<div id="content"></div>
<aside></aside>
<footer>
<ul>
<li></li>
</ul>
</footer>
Run Code Online (Sandbox Code Playgroud)
现在我想将页脚准确放在左下角:
text-align: left;
position: fixed;
bottom: 0;
margin: -16px -8px;
Run Code Online (Sandbox Code Playgroud)
现在是负边际不是最好的解决方案.是否有更好的方法将页脚直接定位在角落?
问候
我想要一个侧边栏,它占据了屏幕的整个高度,并且在向下滚动时不会改变.
因此,当我仅滚动内容更改但导航栏始终保持不变时.
我做了第一个例子:http://jsfiddle.net/CwSD6/
但是缺少非滚动功能......
问候
编辑:您需要使用位置:固定,顶部,底部,左侧值为0. http://jsfiddle.net/CwSD6/1/
当我浏览GitHub时,大多数项目都以这种方式在接口中定义方法:
interface ExampleInterface
{
function getId();
}
Run Code Online (Sandbox Code Playgroud)
我现在的问题是为什么在接口中定义方法可见性是不好的风格:
interface ExampleInterface
{
public function getId();
}
Run Code Online (Sandbox Code Playgroud)
它使接口更严格,但不是一个接口用于什么?
我目前使用以下设置来注册新用户:
// creates a new user
app.post('/users', function(req, res) {
// create new user
var user = new User();
// assign post
user.username = req.body.username;
user.email = req.body.email;
crypto.randomBytes(32, function(err, buf) {
if (err) throw err;
user.salt = buf.toString('hex');
crypto.pbkdf2(req.body.password, user.salt, 25000, 512, function(err, encodedPassword) {
if (err) throw err;
user.password = (encodedPassword.toString('hex')); // this line
user.save(function(err, user) {
if (!err) return res.send(err, 500);
return res.json(user);
});
}.bind(this));
});
});
Run Code Online (Sandbox Code Playgroud)
仔细看看这一行:
user.password = (encodedPassword.toString('hex'));
Run Code Online (Sandbox Code Playgroud)
这应该将密码字符串(看起来像二进制字符串)编码为十六进制字符串.出于某种原因,这不起作用.
为什么不?
Byside:盐和密码存储的推荐编码是什么(十六进制,二进制,base64)?
我想用查询字符串在我的用户存储库中搜索.
这应该返回所有用户名为"kyogron"的用户和类似的电子邮件"kyogron @gmail"
GET localhost:3000/users?username=kyogron&email=kyogron@gmail.com
Run Code Online (Sandbox Code Playgroud)
这应该返回所有用户:
GET localhost:3000/users
Run Code Online (Sandbox Code Playgroud)
我已经设法处理路由参数,但我坚持优化它:
app.get('/users', function(req, res) {
// creates a mongoosejs query object
var query = User.find({});
// to understand how expressjs handles queries:
// ?username=kyogron&email=kyogron@gmail.com
// { username: "kyogron", email: "kyogron@gmail.com" }
//console.log(req.query);
// this was one idea of optimizing the search query parameters
// this doesn't work don't know why I always get an array of ALL users
// even the key and value is right
Object.keys(req.query).forEach(function(key) {
query.select(key, req.query[key]);
});
// …Run Code Online (Sandbox Code Playgroud) 我想知道嵌套Backbone Views的推荐方法是什么.
嵌套视图的可能方法:
我已经在这个小提琴中试过我的运气:http://jsfiddle.net/m48Nc/2/
注释:我知道这个例子不起作用它只是显示结构,我现在想出来了,但对此并不满意.
那么走哪条路呢?也欢迎链接;)
更新:
使用fguillen答案和我发现我们可以做的另一个线程:
var IndexView = Backbone.View.extend({
tagName: "div",
className: "container",
template: LayoutTemplate,
render: function() {
this.$el.html(LayoutTemplate);
this.$('div.content').html(ContentTemplate);
this.$('div.sidebar').append(new LoginView().render().el);
this.$('div.sidebar').append(new RegistrationView().render().el);
return this;
}
});
Run Code Online (Sandbox Code Playgroud) 我已经定义了多个路由中间件,并希望在多个路由/控制器之间共享它们.
这是我的设置:
app.js需要./routes/index.js:
// load fs module
var fs = require('fs');
// import routing files
module.exports = function(app){
fs.readdirSync(__dirname).forEach(function(file) {
if (file == "index.js") return;
var name = file.substr(0, file.indexOf('.'));
require('./' + name)(app);
});
};
Run Code Online (Sandbox Code Playgroud)
index.js在dir中自动加载所有路由.可能的路径文件可能如下所示:
module.exports = function(app) {
app.get('/contacts', function(req, res, next) {
// routing stuff
});
};
Run Code Online (Sandbox Code Playgroud)
现在我得到了路由中间件:
function isAuthenticated(req, res, next) {
if (!req.session.authenticated) return next(new Error('user not authenticated'));
};
function loadUser(req, res, next) {
var query = User.findById(req.session.user_id);
query.populate('contacts');
query.exec(function(err, user) {
if (err) return …Run Code Online (Sandbox Code Playgroud) 我有一个包含8个字节的缓冲区对象.现在应将这八个字节解释为64位整数.
目前我使用以下算法:
var int = buff[0];
for (var i = 1; i < buff.length; i++) {
int += (buff[i] * Math.pow(2, 8 * i));
}
console.log(int);
Run Code Online (Sandbox Code Playgroud)
这有效,但我相信有更好的方法(也许使用Uint64Array).
不幸的是我无法找到Uint16Array如何帮助我.
问候
更新:
// puts two 32bit integers to one 64bit integer
var bufInt = (buf.readUInt32BE(0) << 8) + buf.readUInt32BE(4);
Run Code Online (Sandbox Code Playgroud) 目前我有一个包含store以下内容的包:
package store
var (
db *Database
)
func Open(url string) error {
// open db connection
}
func FindAll(model interface{}) error {
// return all entries
}
func Close() {
// close db connection
}
Run Code Online (Sandbox Code Playgroud)
这允许我在完成store.FindAll后可以从其他包中使用。store.Openmain.go
然而,据我所知,到目前为止,大多数包更喜欢提供一个您需要自己初始化的结构。使用这种全局方法的情况很少。
这种方法有哪些缺点?我应该避免吗?
javascript ×6
node.js ×4
express ×3
css ×2
html ×2
backbone.js ×1
buffer ×1
byte ×1
coding-style ×1
cryptography ×1
encoding ×1
go ×1
integer ×1
interface ×1
middleware ×1
mongoose ×1
php ×1
position ×1
session ×1
singleton ×1