[英]Cookies and session nodejs
I created Api server using nodejs and the client side using reactjs, the client run in url:5000 and client run to url:3000, Run separately. I created Api server using nodejs and the client side using reactjs, the client run in url:5000 and client run to url:3000, Run separately. A client party sends requests to a server side and it returns to it.
客户端向服务器端发送请求并返回给它。 i create a session in server, when i connected using login function i return some token using jwt, and i return the token to client, but the problem in client browser i don't have a session.id
i create a session in server, when i connected using login function i return some token using jwt, and i return the token to client, but the problem in client browser i don't have a session.id
this is my code appjs这是我的代码 appjs
const express = require('express');
const app = express();
const session = require('express-session');
const cors = require('cors');
const cookieParser = require('cookie-parser')
const resource = require('./resources/en-US');
const dotenv = require('dotenv');
dotenv.config();
var corsOptions = {
origin: 'http://localhost:3000',
credentials: true,
optionsSuccessStatus: 200 // For legacy browser support
}
const authRouter = require('./routes/auth-router');
const adminRouter = require('./routes/admin-router');
app.use(cors(corsOptions));
app.use(express.json());
// app.use(cookieParser());
const http = require('http').createServer(app);
const mongoose = require('mongoose');
const MongoStore = require('connect-mongodb-session')(session);
const store = new MongoStore({
uri : process.env.MONGODB_URL,
collection:'session',
});
app.use(session({
secret: process.env.SESSION_SECRET_KEY,
resave: true,
saveUninitialized: true,
store: store,
cookie: { secure: true,httpOnly:true }
}))
app.use('/api', authRouter);
app.use('/api', adminRouter);
mongoose
.connect(process.env.MONGODB_URL,{
useNewUrlParser:true,
useCreateIndex:true,
useUnifiedTopology:true
})
.then(() => console.log('connected'))
.catch(err => console.log(err))
http.listen(process.env.PORT, () => {
console.log(`listening on port ${process.env.PORT}`);
});
and when i go to other route in Api server, and try to print the req.session i don see my session with store data. and when i go to other route in Api server, and try to print the req.session i don see my session with store data.
please i need a help, What is the problem?请问我需要帮助,有什么问题?
this is a client request:这是一个客户请求:
export const authenticationLogin = async (data) => {
const {email , password} = data;
console.log("log in controller")
try{
return new Promise( (resolve,reject) => {
axios(
{
url:"/login",
method : "POST",
data: {
email : email,
password: password
},
withCredentials: true
}
).then( async (data) => {
console.log("after responce feching")
axios.defaults.headers.common['x-auth-token'] = data.data.token;
console.log(data);
debugger;
resolve(data.data);
}).catch(error => {
reject(error)
})
})
}catch(error){
console.error(error);
}
}
this is not work added new image with response headers, i see the setCookies but i cant see it in cookies (Application tab )这不起作用添加了带有响应标题的新图像,我看到了 setCookies,但我在 cookies(应用程序选项卡)中看不到它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.