这是我的代码
const express =require('express');
const bodyParser=require('body-parser');
const User=require('./models/User');
const mongoose=require('mongoose');
const cookieParser = require('cookie-parser');
const session = require('express-session');
const MemcachedStore = require("connect-memcached")(session);
const app=express();
app.use(cookieParser());
var MemoryStore =session.MemoryStore;
app.use(session({
name : 'app.sid',
secret: "1234567890QWERTY",
resave: true,
store: new MemoryStore(),
saveUninitialized: true
}));
mongoose.connect("mongodb://localhost:27017/alpha")
.then(()=>{
console.log("Connected to database")
})
.catch((err)=>{
console.log("Error occureed");
throw err;
})
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended:false}));
app.use((req,res,next)=>{
res.setHeader('Access-Control-Allow-Origin','*');
res.setHeader('Access-Control-Allow-Headers',
'Origin,X-Requested-With,Content-Type,Accept');
res.setHeader('Access-Control-Allow-Methods','GET,POST,PATCH,DELETE,OPTIONS');
next();//calls the next one
console.log("hello");
});
app.post('/api/post',(req,res)=>{
req.session.name='kk';
req.session.email=req.body.email;
req.session.password=req.body.password;
req.session.phonenumber=req.body.phonenumber;
console.log(req.session.email);//this works
res.json({
message:'Data catched by server'
});
});
app.get('/api/post',(req,res)=>{
res.send("hello"+req.session.email);//this does'nt works
console.log(req.session.email);
});
module.exports=app;
我正在通过body解析器保存请求参数值,并在get request中输出它们,但是它显示了未定义的数据,同时在浏览器或控制台中输出了数据,并记录了如何实现这一点? ,我已经在有效的代码和无效的代码中进行了注释。