req.session.destroy()在Nodejs中不起作用

时间:2017-04-05 12:17:45

标签: node.js session express

尝试通过在Nodejs中使用 req.session.destroy()销毁会话来执行注销操作,但它无法正常工作。

Nodejs代码:

router.get('/logout', function(req, res) {
  req.session.destroy();
  console.log(session.email);
  res.redirect('/signup');
});

我从我的html文件调用/ logout api。

HTML代码: -

<div class="container-fluid">
        <header class="row top text-center">
            <div class="col-md-2 righttopnav" >
                <a href="index" style="font-size:25px;">Auto<b>SecT</b></a>
            </div>
            <div class="col-md-10 topnav" id="myTopnav">
                <%if(session.email){%>
                <a href="/logout">Logout</a>
                <% } else { %>
                <a href="/signup">Signup / Login</a>
                <% } %>
                <a href="#career">Career</a>
                <a href="#clients">Clients</a>
                <a href="#services">Services</a>
            </div>
        </header>

        <section class="content">   
            <div class="row text-center">
                <div class="col-md-12 " >
                    <p class="p1"><%-session.email%>Scan your Website or APP with just a <b>click!</b></p>
                </div>
            </div>
        </section>

        <div class="row text-center" style="padding-top:5%;">
            <div class="col-md-4">
                <i class="fa fa-globe fa-5x" aria-hidden="true" style="color:#c2c2a3"></i>
                <br>
                <input class="url" type="url"  placeholder="Enter your url" style="border-radius: 20px ">
            </div>  
            <div class="col-md-4">
                <p class="p">OR</p>
            </div>
            <div class="col-md-4">
                <i class="fa fa-file-o fa-5x" aria-hidden="true" style="color:#c2c2a3; padding-bottom: 4% "></i>
                <br>
                <label class="btn btn-primary btn-sm  btn-file upload">
                <i class="fa fa-upload fa-2x" aria-hidden="true"></i>
                <input type="file" style="display: none;">
                </label>
            </div>
        </div>
        <div class="row">
            <div class="col-md-12">
                <div class="col-md-12 text-center" style="padding-top: 5%; margin-bottom: 1%; border-radius: 20%;">
                <input type="submit" id="button" class="btn btn-success btn-md" value="Scan">
            </div>
            </div>
        </div>
</div>

/ logout将我重定向到所需的页面,但会话不会被破坏。

5 个答案:

答案 0 :(得分:3)

请在会话销毁后重定向。 您必须在回调中移动代码以进行重定向。

package com.hortonworks.example.kafka.consumer;
import org.apache.kafka.clients.CommonClientConfigs;
import org.apache.kafka.clients.consumer.ConsumerConfig;
import org.apache.kafka.clients.consumer.ConsumerRebalanceListener;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.apache.kafka.clients.consumer.ConsumerRecords;
import org.apache.kafka.clients.consumer.KafkaConsumer;
import org.apache.kafka.common.TopicPartition;
import java.util.Collection;
Configuring Kafka for Kerberos
Over Ambari
May 9, 2016
9
import java.util.Collections;
import java.util.Properties;
public class BasicConsumerExample {
public static void main(String[] args) {`enter code here`
Properties consumerConfig = new Properties();
consumerConfig.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka.
example.com:6667");
// specify the protocol for SSL Encryption
consumerConfig.put(CommonClientConfigs.SECURITY_PROTOCOL_CONFIG,
"SASL_PLAINTEXT");
consumerConfig.put(ConsumerConfig.GROUP_ID_CONFIG, "my-group");
consumerConfig.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG,
"earliest");
consumerConfig.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
"org.apache.kafka.common.serialization.StringDeserializer");
consumerConfig.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, "org.
apache.kafka.common.serialization.StringDeserializer");
KafkaConsumer<byte[], byte[]> consumer = new
KafkaConsumer<>(consumerConfig);
TestConsumerRebalanceListener rebalanceListener = new
TestConsumerRebalanceListener();
consumer.subscribe(Collections.singletonList("test-topic"),
rebalanceListener);
while (true) {
ConsumerRecords<byte[], byte[]> records = consumer.poll(1000);
for (ConsumerRecord<byte[], byte[]> record : records) {
System.out.printf("Received Message topic =%s, partition =%s,
offset = %d, key = %s, value = %s\n", record.topic(), record.partition(),
record.offset(), record.key(), record.value());
}
consumer.commitSync();
}
}
private static class TestConsumerRebalanceListener implements
ConsumerRebalanceListener {
@Override
public void onPartitionsRevoked(Collection<TopicPartition> partitions)
{
System.out.println("Called onPartitionsRevoked with partitions:" +
partitions);
}
@Override
public void onPartitionsAssigned(Collection<TopicPartition> partitions)
{
System.out.println("Called onPartitionsAssigned with partitions:" +
partitions);
}
}
}

答案 1 :(得分:1)

尝试销毁会话并注销:

SET DEFAULT CURRENT_TIMESTAMP

答案 2 :(得分:1)

这对我有用。

.get('/logout', function (req, res) {
    req.logout();
    req.session = null; 
    res.redirect('/back/to/login');
});

答案 3 :(得分:1)

此解决方案适用于我:

req.session.destroy(function () {
    req.logout();
    res.redirect('/');              
});

答案 4 :(得分:1)

我做了两件事,使它奏效了。首先,我在会话选项中将未设置为 destroy 。其次,删除会话,如下所示

router.get('/logout', function(req, res) {
        delete req.session;
        res.redirect('/signup');
    });

ref https://github.com/expressjs/session