Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
403 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 Answer

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()); 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...