小编Jac*_*ack的帖子

使用Node中的MemoryStore存储会话数据,类似于$ _SESSION ['data'] = PHP中的值

题:

是否有可能在PHP中存储类似于$ _SESSION ['somedata'] ="Hello"的会话数据?

到目前为止,这是我的代码:

创建内存存储

var MemoryStore = express.session.MemoryStore,
    sessionStore = new MemoryStore();
Run Code Online (Sandbox Code Playgroud)

快速配置

app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(express.cookieParser());
  app.use(express.session({
    store: sessionStore,
    secret: 'secret', 
    key: 'express.sid'}));
  app.use(require('stylus').middleware({src: __dirname + '/public'}));
  app.use(app.router);
  app.use(express.static(__dirname + '/public'));
});
Run Code Online (Sandbox Code Playgroud)

解析cookie以获取握手授权的会话ID

var parseCookie = require('connect').utils.parseCookie;

io.set('authorization', function (data, accept) {
  if (data.headers.cookie) {
    data.cookie = parseCookie(data.headers.cookie);
    data.sessionID = data.cookie['express.sid'];

    sessionStore.get(data.sessionID, function (err, session) {
      if (err) 
      {
          accept(err.message, false); //Turn down the connection
      } 
      else
      {
          data.session …
Run Code Online (Sandbox Code Playgroud)

node.js express socket.io

5
推荐指数
1
解决办法
9488
查看次数

在JavaScript类函数中使用setTimeout()

是否可以在JavaScript对象中使用setTimout()?

目前动画方法调用运行一次,似乎setTimeout()没有完成它的工作.我已经设法让它工作,但是在一个非常hackish方法中,在类之外使用setTimeout.我想让动画循环成为AnimationManager类的工作.如果你能看到任何不良做法,或者我出错了......请给我一个抬头!

JavaScript的:

var AnimationManager = function(canvas)
{
    this.canvas = canvas;
    this.canvasWidth = canvas.width();
    this.canvasHeight = canvas.height();
    this.ctx = canvas.get(0).getContext('2d');
    this.running = true;

    this.start = function start(){
        this.running = true;
        this.animate();
    }

    /** Allow the animations to run */
    this.run = function run(){
        this.running = false;

    } 
    /** Stop the animations from running */    
    this.stop = function stop(){
        this.running = false;
    }

    this.animate = function animate()
    {
        if(this.running)
        {
            this.update();
            this.clear();
            this.draw();
        }
        setTimeout(this.animate, 40); //25 fps
    }

    /** Update …
Run Code Online (Sandbox Code Playgroud)

javascript oop settimeout javascript-objects

4
推荐指数
1
解决办法
4785
查看次数

Nodejs安装不起作用,[Errno 13]权限被拒绝:'/ usr/local/include/node /'

我正在尝试使用以下命令安装Node,socket.io和npm:

cd ~
sudo apt-get install libv8-2.0.3 libv8-dev libv8-dbg libssl-dev curl
wget http://nodejs.org/dist/node-v0.4.3.tar.gz
tar zxvf node-v0.4.3.tar.gz
cd node-v0.4.3
sudo ./configure && make && make install
sudo curl http://npmjs.org/install.sh | sudo sh
sudo npm install socket.io
Run Code Online (Sandbox Code Playgroud)

但是脚本失败了:

sudo ./configure && make && make install
Run Code Online (Sandbox Code Playgroud)

它无法在usr/local/include中创建节点目录.我正在使用sudo而且它不能正常工作...我有一种感觉,它与我从Windows安装的Ubuntu 11.04安装有关...但我可能是错的.当我去usr/local/include我没有权限做任何事情,即使我有root权限....

你知道为什么这不起作用吗?

这是完整的错误消息:

Cannot create folder '/usr/local/include/node/' (original error: [Errno 13] Permission denied: '/usr/local/include/node/')
Run Code Online (Sandbox Code Playgroud)

ubuntu node.js npm socket.io

4
推荐指数
1
解决办法
7158
查看次数

将html元素传递到jade文件中

是否可以将HTML元素传递到jade文件中,例如我希望以下内容用文本填充p元素,以及包含嵌套在p元素内的一些包含文本的代码元素.

包含字符串的JSON

var news = {
  one : {
    title : "Using JSON",
     body : "Using JSON is a great way of passing information into the jade template files."+
            "It can be looped over using jades each syntax <code>test</code>"
  },
  two : {
    title : "",
    body : ""
  }
}
Run Code Online (Sandbox Code Playgroud)

路由HTTP请求

// Routes
app.get(navigation.home.uri, function(req, res){ //Home
  res.render(navigation.home.url, {
    title: navigation.home.title,
    navigation: navigation,
    news: news
  });
});
Run Code Online (Sandbox Code Playgroud)

Jade文件片段,每个新闻项都有一个循环

  section#news
    - each item in news
      article(class="news")
        header
          h2 #{item.title}
        p …
Run Code Online (Sandbox Code Playgroud)

javascript node.js express pug

4
推荐指数
1
解决办法
4285
查看次数

如何使用Express和Node.js的slug?

如何从去:http://url:port/user?u=usernamehttp://url:port/user/username节点服务器上使用快递框架时?

当前的JavaScript服务器端代码:

  app.get('/user', function(req, res){
    var username = req.param("u");    
    users.findOne({username : username}, function(err, result){      
      var user = {
        username : result.username,
        name : result.name,
        surname : result.surname,
        email : result.email     
      }

      res.render('user',
        {user : user}
      );
    })
  })
Run Code Online (Sandbox Code Playgroud)

javascript node.js express

3
推荐指数
1
解决办法
2945
查看次数

创建控制器类时未显示模型

由于某种原因,添加控制器类对话框没有拾取电影和MovieDBContext分类.我是ASP.NET和Visual Studio 2010的新手,我应该为这些类在这里展示一些东西吗?

截图

c# asp.net

3
推荐指数
1
解决办法
2113
查看次数

计算屏幕外地图位置的半径

题:

如何考虑屏幕的矩形形状以确保圆圈精确地绘制到填充边界?

以下适用于"圆形"区域,但不适用于矩形区域......

dx = abs(center.x - place.x);
dy = abs(center.y - place.y);
dh = Math.sqrt((dx * dx) + (dy * dy));
radius = dh - padding;
Run Code Online (Sandbox Code Playgroud)

光环设计: 光环的想法

如果问题似乎不明显,下面的图像代表我正在使用的当前方法.取决于地点的位置影响它伸入屏幕空间的距离. 在此输入图像描述

math android google-maps

3
推荐指数
1
解决办法
837
查看次数

实体框架EntityType'UserAccount'没有定义键

我收到以下错误:

One or more validation errors were detected during model generation:   
\tSystem.Data.Entity.Edm.EdmEntityType: : EntityType 'UserAccount' has no key defined. Define the key for this EntityType.
\tSystem.Data.Entity.Edm.EdmEntitySet: EntityType: EntitySet 'UserAccounts' is based on type 'UserAccount' that has no keys defined.
Run Code Online (Sandbox Code Playgroud)

此错误由代码触发:

_Db.Database.Initialize(true);
Run Code Online (Sandbox Code Playgroud)

我假设由于某种原因它没有在模型上获取[Key]属性.最初当我尝试运行代码时,我没有添加key属性,这是否意味着已经创建/缓存了某些东西,这阻止了这个密钥的应用?

除了包含Entity Framework 5之外,MVC4项目几乎是一个空白的设置

型号代码

public class AccountContext: DbContext
{
    public AccountContext()
        : base("DefaultConnection")
    {          
    }

    public DbSet<UserAccount> UserAccounts { get; set; }
}

[Table("UserAccount")]
public class UserAccount
{
    [Key]
    [Required]        
    public string Username;

    [Required]
    [DataType(DataType.Password)]
    [StringLength(100, ErrorMessage …
Run Code Online (Sandbox Code Playgroud)

entity-framework asp.net-mvc-4

3
推荐指数
1
解决办法
1万
查看次数

HttpContext.User.Identity.IsAuthenticated永远不会是真的

登录

[HttpPost]
public ActionResult Login(LoginModel loginModel)
{
    if (ModelState.IsValid)
    {
        using (var _Db = new AccountContext())
        {
            var _UserAccount = _Db.UserAccounts.FirstOrDefault(u => u.Username == loginModel.Username && u.Password == loginModel.Password);

            if (_UserAccount == null)
            {
                ModelState.AddModelError("", "Account doesn't exist!");
            }
            else
            {
                FormsAuthentication.SetAuthCookie(loginModel.Username, false);
            }
        }
    }
    return RedirectToAction("Index", "Home");
}
Run Code Online (Sandbox Code Playgroud)

重定向或显示视图

public ActionResult Index()
{
    if (HttpContext.User.Identity.IsAuthenticated)
    {
        return View("Index");
    }
    else
    {
        return RedirectToAction("LoginPage");
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经完成了代码,可以看到使用正确的用户名调用SetAuthCookie. FormsAuthentication.SetAuthCookie(loginModel.Username, false);

什么可以阻止用户进行身份验证?

c# forms-authentication asp.net-mvc-4

3
推荐指数
1
解决办法
6894
查看次数

MSBuild"GenerateFakes"错误MSB4127,MSB4060

使用Visual Studio 2013构建GenerateFakes成功时,它使用相同目标文件的相同路径.

通过MSBuild 12.0(Visual Studio 2013附带的相同版本)构建时,我收到以下两个错误.

错误#1

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Fakes\Microsoft.QualityTools.Testing.Fakes.targets(128,5): error MSB4127: The "GenerateFakes" task could not be instantiated from the assembly "C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Fakes\Microsoft.QualityTools.Testing.Fakes.Tasks.dll". Please verify the task assembly has been built using the same version of the Microsoft.Build.Framework assembly as the one installed on your computer and that your host application is not missing a binding redirect for Microsoft.Build.Framework. Unable to cast object of type 'Microsoft.QualityTools.Testing.Fakes.GenerateFakes' to type 'Microsoft.Build.Framework.ITask'. 
Run Code Online (Sandbox Code Playgroud)

错误#2

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v12.0\Fakes\Microsoft.QualityTools.Testing.Fakes.targets(128,5): error MSB4060: The "GenerateFakes" …
Run Code Online (Sandbox Code Playgroud)

msbuild visual-studio microsoft-fakes visual-studio-2013

3
推荐指数
1
解决办法
3609
查看次数