Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
411 views
in Technique[技术] by (71.8m points)

javascript - Session management in Nodejs

I am beginner of NodeJS.And just started a simple project where I need a session management concept. So How to manage the session in NodeJS application.

In my project there is two file:- app.js and routes.js.

So where we add the session and how to add ??

app.js file :-

var express = require('express'),
    app = express(),
    path = require('path');


app.set('views', path.join(__dirname , 'views'));
 
app.engine('html', require('hogan-express'));   
  
app.set('view engine', 'html');
  
app.use(express.static(path.join(__dirname,'public')));

require('./routes/routes.js')(express,app);

app.listen (3000 , function(){
    console.log("working on the Port 3000"); 
   
 });
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

For the session management we need a middleware 'cookie-parser'.Previously it is the part of express but after express 4.0 and later it is a separate module.

So to access the cookie parser we need to install in our project as :

npm install cookie-parser --save

Then add this into your app.js file as :

var cookieParser = require('cookie-parser');

 app.use(cookieParser()); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...