小编use*_*284的帖子

Node.js /删除文件中的内容

我想用node.js删除一个简单文本文件的内容.或者用新/空文件替换文件.

我怎样才能在节点中实现这一点?

node.js

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

Passport + Node.js /添加用户后自动登录

我使用护照进行身份验证和会话处理.到目前为止一切正常.我实施了一个"登录"表单,以便向应用添加新用户.添加用户后,我想自动登录他/她.

实现这一目标的最佳方法是什么 - 我应该使用用户凭据重定向到"/ login"还是有另一种/更好的方法(调用serializeUser)来做到这一点?

到目前为止,我认为我并不真正理解"完成"功能(在serializeUser和LocalStrategy中)的工作方式或它正在做什么......

这是我的代码:

passport.serializeUser(function(user, done) {
    done(null, user._id);
});
passport.deserializeUser(function(id, done) {
    authProvider.findUserById('users', id, function (err, user) {
        done(err, user);
    });
});

passport.use(new LocalStrategy( function(email, password, done) {
    authProvider.getUserByEmail('users', email, function(error, user){
        if(error) { return done(error); }
        if (!user) { return done(null, false, { message: 'Unknown user ' + email });}   
        if (user.password != password) { return done(null, false);}
        return done(null, user);
        });
    }
));

app.post('/login', 
    passport.authenticate('local', { failureRedirect: '/login'}),
    function(req, res) { res.redirect('/');});

app.post('/sign', function(req, …
Run Code Online (Sandbox Code Playgroud)

javascript node.js passport.js

31
推荐指数
2
解决办法
2万
查看次数

DynamicDrawable以编程方式没有animation-list xml

我处于一种我希望从动态图像列表中显示动画的情况.我想为此目的使用AnimationDrawable,但不幸的是我被困... :(我的代码是

ImageView globe = (ImageView) findViewById(R.id.globe);

AnimationDrawable animation = new AnimationDrawable();
animation.setOneShot(true);

Resources res = this.getResources();

while (from <= to) {
    String name = "globe_" + String.format("%03d", from);
    int globeId = res.getIdentifier(name, "drawable",
            this.getPackageName());
    animation.addFrame(res.getDrawable(globeId), 200);
    from++;
}

globe.setBackgroundDrawable(animation);
globe.post(new Runnable(){
        @Override
        public void run() {
            animation.start();
        }
});
Run Code Online (Sandbox Code Playgroud)

android android-animation

13
推荐指数
2
解决办法
2万
查看次数

使用Golang中的database/sql包调用QueryRow方法的超时

QueryRow使用database/sqlGolang中的包实现方法调用超时的适当方法是什么?关于这个主题有很多讨论,我想知道golang 1.7中是否有解决方案/最佳实践,除了使用context这里描述的包:

能够在从池中连接时进行超时

此外,最近似乎已经实施上下文支持.使用上下文来超时连接的适当方法是什么?

go

8
推荐指数
1
解决办法
1907
查看次数

如何将golang请求中的上下文传递给中间件

我试图了解Golang 1.7中引入的上下文如何工作,以及将其传递给中间件和.NET的适当方法是什么HandlerFunc。上下文应该在主函数中初始化并传递给checkAuth函数吗?以及如何将其传递给HanlderServeHTTP函数?我读了Go并发模式如何使用上下文,但是我很难使这些模式适应我的代码。

func checkAuth(authToken string) util.Middleware {
    return func(h http.Handler) http.Handler {
        return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
            if r.Header.Get("Auth") != authToken {
                util.SendError(w, "...", http.StatusForbidden, false)
                return
            }
            h.ServeHTTP(w, r)
        })
    }
}

// Handler is a struct
type Handler struct {
    ...
    ...
}

// ServeHTTP is the handler response to an HTTP request
func (h *HandlerW) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    decoder := json.NewDecoder(r.Body)

    // …
Run Code Online (Sandbox Code Playgroud)

design-patterns timeout request go

5
推荐指数
4
解决办法
5744
查看次数

玉模板引擎/如何设置背景图像

我想将背景图像设置为我用Jade渲染的HTML页面.有谁知道如何实现这一目标?

谢谢

html pug

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

修剪词典

我想知道是否有一种更简单/ pythonic的方式来修剪python中字典的"大小/长度".如果我有一个包含10个键值对(元素)的dict,并且我想将大小限制为5.在循环中删除元素是最佳解决方案(顺序/标识无关紧要)

def _trim_search_results(self):

    MAX_RESULTS = 5

    # a big dict
    results_to_be_trimmed = {result_1 ... result_n}

    # dict with length MAX_RESULTS
    trimmed_results = {}

    for index, key in enumerate(results_to_be_trimmed):
        if index == MAX_RESULTS:
            break
        else:
            trimmed_results[key] = results_to_be_trimmed[key]

    results_to_be_trimmed = trimmed_results
Run Code Online (Sandbox Code Playgroud)

我认为必须有更好的解决方案......

python

4
推荐指数
2
解决办法
6165
查看次数

扭曲/执行异步http请求

我有一个扭曲的反应堆,监听传入的数据。我有第二个反应堆,在一定的时间间隔内执行http请求,将结果发送到第一个反应堆。两者都运行良好。

现在,我想将其组合在一起以在一个反应​​堆中运行,但是我不知道如何实现这一目标。类似于-每60秒执行一次HTTP请求。在第一个侦听“主”反应堆内以不规则的方式

我目前所拥有的是:

# main reactor listening for incoming data forever
...
reactor.listenTCP(8123, TCPEventReceiverFactory())
Run Code Online (Sandbox Code Playgroud)

http反应堆用于twisted.internet.defer.DeferredSemaphore()执行几个http检查:

# create semaphore to manage the deferreds
semaphore = twisted.internet.defer.DeferredSemaphore(2)

# create a list with all urls to check
dl = list()
# append deferreds to list
for url in self._urls:
    # returns deferred
    dl.append(semaphore.run(self._getPage, url))

# get a DefferedList
dl = twisted.internet.defer.DeferredList(dl)
# add some callbacks for error handling
dl.addCallbacks(lambda x: reactor.stop(), self._handleError)

# start the reactor    
reactor.run()
Run Code Online (Sandbox Code Playgroud)

如何将定时的http检查添加到“主”反应堆中,以便它们以异步方式执行?究竟如何DeferredSemaphore运作?

谁能帮我这个? …

python asynchronous twisted python-2.7

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

以Pythonic方式将项添加到列表中

我想知道是否有更多pythonic方法将元素添加到列表中,具体取决于(默认)索引.如果索引超出范围会发生什么.(我来自Java)

self.releases = []

def add_release(self, release, index=-1):

    list_length = len(self.releases)

    if index < 0:
        # add element at the end of the list
        self.releases.append(release)
    elif index < list_length:
        # add element at the given index
        self.releases.insert(index, release)
    else:
        # index is out of bound ~ what will happen when an element will be added at this index?
        pass
Run Code Online (Sandbox Code Playgroud)

提前致谢.

python list

2
推荐指数
1
解决办法
158
查看次数

从字符串中提取可变长度的数量

我想从字符串中提取一些可变长度.

字符串看起来像这样:

used_memory:1775220696
Run Code Online (Sandbox Code Playgroud)

我想把这个1775220696部分放在一个变量中.关于这个问题有很多问题,但我找不到适合我需求的解决方案.

regex bash

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

Python单例再次/如何使用类属性?

我想在 Python 中有一个带有 Java 的单例类,例如“静态类属性”。我阅读了关于 Python 单例的几篇文章,除了使用简单的模块作为单例之外,找不到解决方案。

有没有办法扩展此代码(PEP318)以将其与我可以从函数访问的“静态类属性”一起使用?

def singleton(cls):
    instances = {}
    def getinstance():
        if cls not in instances:
            instances[cls] = cls()
        return instances[cls]
    return getinstance

@singleton
class MyClass:
    ...
Run Code Online (Sandbox Code Playgroud)

python

0
推荐指数
1
解决办法
2290
查看次数