简体   繁体   中英

Node.js GET doesn't seem to be working

I'm a beginner to Node.js. I'd be thankful if you'd give me some advice.

I made a sample app as the following.

var express = require('express'), 
app = express(), 
morgan = require('morgan'), 
bodyParser = require('body-parser'), 
cookieParser = require('cookie-parser'),
session = require('express-session'),
port = 3000;

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(morgan('combined'));

app.use(cookieParser);
app.use(session({
    secret: 'secret', 
    saveUninitialized: true,
    resave: true
}));

app.get('/', function(req, res){ 
    var response = '<form method="post" action="/"><input name="remember"><button type="submit">Submit</button></form>';

    if (req.session.remember){
        response += '<h1>' + req.session.remember + '</h1>';
    }

    res.send(response);
});

app.post('/', function(req, res){
    req.session.remember = req.body.remember;
    res.redirect('/');
});

app.listen(port, function(err){
    console.log('listening on %s', port);
});

Somehow, the page doesn't show. It looks like GET method doesn't seem to be working.

Maybe my usage of Session is wrong? I have no idea.

Could you tell me the reasons?

它应该是

app.use(cookieParser());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM