Session using NodeJS and Express 4.x (req.session undefined) - node.js

First of all sorry my bad English. I'm studying and I'm doing an academic project and came across a problem. I can not recall a session variable.
package.json
"dependencies": {
"body-parser": "^1.7.0",
"ejs": "~1.0.0",
"express": "^4.8.7",
"express-session": "^1.11.3",
"ini": "^1.3.4",
"mysql": "^2.8.0",
"winston": "^1.0.1"
}
server.js
var express = require('express');
var session = require('express-session');
var app = express();
...
require('./router/main')(app, language, connection, logger);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.engine('html', require('ejs').renderFile);
app.use(express.static(__dirname + '/public'));
app.use(session({
secret: 'pecuniamsekretsession',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
}));
main.js
module.exports = function(app, languageItems, connection, logger) {
app.post('/', require('../router/login.js')(languageItems, connection, logger));
app.get('/', require('../router/login.js')(languageItems, connection, logger));
}
login.js
module.exports = function (language, connection, logger) {
var routerTemplateLogin = function (req, res) {
var utils = require('../utils/utils');
var util = new utils.Util;
// It does not work :-(
logger.debug(req.session);
req.on('data', function(data) {
var arrayPost = util.postDataToArray(data);
});
res.render('pages/login.ejs', {
lang: language
});
}
return routerTemplateLogin;
}
In login.js see // It does not work :-(. Full project in https://github.com/braulioti/pecuniam
Thanks

The problem is that the routes are set up before any other middleware. Move this line:
require('./router/main')(app, language, connection, logger);
after this:
app.use(session({
secret: 'pecuniamsekretsession',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
}));
in your server.js.

Related

Am I looking for help about i118 express node.js?

When i choose some language, i get error message . that err msg is .translation is not defined.
example :
http://localhost:5000/?clang=en
Can I achieve my goal with just these 23 files or do I have to use other methods as well?
my app.js code is
// Imports
const express = require('express')
const expressLayouts = require('express-ejs-layouts')
const app = express()
const port = 5000
// import Router file
var session = require('express-session');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var i18n=require("i18n-express"); // <-- require the module
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
secret: 'secret',
saveUninitialized: true,
resave: true
}));
app.use(i18n({
translationsPath: path.join(__dirname, 'i18n'), // <--- use here. Specify translations files path.
siteLangs: ["es", "en", "de", "ru", "it"],
textsVarName: 'translation'
}));
// Static Files
app.use(express.static('public'))
app.use('/css', express.static(__dirname + 'public/css'))
app.use('/images', express.static(__dirname + 'public/images'))
// Set Templating Engine
app.use(expressLayouts)
app.set('layout', './layouts/full-width')
app.set('view engine', 'ejs')
// Routes
app.get('', (req, res) => {
res.render('index', { title: 'Home Page'})
})
app.get('/about', (req, res) => {
res.render('about', { title: 'About Page', layout: './layouts/sidebar' })
})
// Listen on Port 5000
app.listen(port, () => console.info(`App listening on port ${port}`))
and my i have follow json file
look at my codes
import i18nMiddleware from "i18next-express-middleware";
import { default as i18n } from "i18next";
import Backend from "i18next-node-fs-backend";
i18n
.use(Backend)
.use(LanguageDetector)
.init({
lng: "en",
whitelist: ["en", "fa"],
fallbackLng: "en",
// have a common namespace used around the full app
ns: ["translation"],
debug: false,
backend: {
loadPath: path.join(__dirname + "/locales/{{lng}}/{{ns}}.json")
// jsonIndent: 2
},
preload: ["en", "fa"]
});
this.app.use(i18nMiddleware.handle(i18n));
path locals/en/translate.js
{
"api": {
"events": {
"email": "email",
"fileMimiType": "File mimi type is not allowed",
"fileSize": "File size Can not greater then "
}
}
call it
i18n.t('api.events.email')

Express4.10 bodyParser req.body undefined

I'm trying to build the login of a node application trying to access the route / login get: req.body undefined
Error:
TypeError: Cannot read property 'usuario' of undefined
at login (/home/makros/workspace/ntalk/controllers/home.js:8:24)
at Layer.handle [as handle_request] (/home/makros/workspace/ntalk/node_modules/express/lib/router/layer.js:82:5)
at next (/home/makros/workspace/ntalk/node_modules/express/lib/router/route.js:100:13)
at Route.dispatch (/home/makros/workspace/ntalk/node_modules/express/lib/router/route.js:81:3)
at Layer.handle [as handle_request] (/home/makros/workspace/ntalk/node_modules/express/lib/router/layer.js:82:5)
at /home/makros/workspace/ntalk/node_modules/express/lib/router/index.js:235:24
at Function.proto.process_params (/home/makros/workspace/ntalk/node_modules/express/lib/router/index.js:313:12)
at /home/makros/workspace/ntalk/node_modules/express/lib/router/index.js:229:12
at Function.match_layer (/home/makros/workspace/ntalk/node_modules/express/lib/router/index.js:296:3)
at next (/home/makros/workspace/ntalk/node_modules/express/lib/router/index.js:190:10)
app.js
var express = require('express'),
load = require('express-load'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
bodyParser = require('body-parser'),
app = express();
load('models')
.then('controllers')
.then('routes')
.into(app);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(cookieParser('secret'));
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static(__dirname + '/public'));
app.listen(3000,function(){
console.log('Started!');
});
package.json
{
"name": "ntalk",
"version": "1.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "^1.9.0",
"cookie-parser": "^1.3.3",
"ejs": "~0.8.5",
"express": ">= 0.0.0",
"express-load": "^1.1.14",
"express-session": "^1.8.2"
}
}
controllers/home.js
module.exports = function(app) {
return {
index: function(req, res) {
res.render('home/index');
},
login: function(req, res){
console.log(req.params);
var email = req.body.usuario.email,
nome = req.body.usuario.nome;
if(email && nome) {
var usuario = req.body.usuario;
usuario['contatos'] = [];
req.session.usuario = usuario;
res.redirect('/contatos');
} else {
res.redirect('/');
}
},
logout: function(req, res){
req.session.destroy();
res.redirect('/');
}
};
};
routes/home.js
module.exports = function(app) {
var home = app.controllers.home;
app.get('/', home.index);
app.get ('/entrar', home.login);
app.get('/sair', home.logout);
};
views/home/index.ejs
<% include ../header %>
<header>
<h1>Ntalk</h1>
<h4>Bem-vindo!</h4>
</header>
<section>
<form action="/entrar" method="post">
<input type="text" name="usuario[nome]" placeholder="Seu nome">
<br>
<input type="text" name="usuario[email]" placeholder="Seu e-mail">
<br>
<button type="submit">Entrar</button>
</form>
</section>
<% include ../footer %>
Please help me!
You're adding your route handlers to app before your body parsing middleware. All routes/middleware/etc. added to app are executed in order in Express 4.
So move this:
load('models')
.then('controllers')
.then('routes')
.into(app);
after this line:
app.use(express.static(__dirname + '/public'));
On an unrelated note, you typically should place your express.static() usage near the top of your route/middleware list for efficiency. This prevents automatic session creation and unnecessary cookie parsing for static asset requests for example.
try move
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
before
load('models')
.then('controllers')
.then('routes')
.into(app);
In fact the problem was in my implementation of express-load, it injects dependencies but does not configure the app, so I decided not to use it:
app.js
var express = require('express'),
path = require('path'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
bodyParser = require('body-parser');
var routes = require('./routes/home'),
contacts = require('./routes/contacts');
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: true
}));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/contatos', contacts);
app.listen(3000,function(){
console.log('Started!');
});
routes/home.js
var router = require('express').Router();
var home = require('../controllers/home');
router.get('/', home.index);
router.post('/entrar', home.login);
router.get('/sair', home.logout);
module.exports = router;
controllers/home.js
module.exports = {
index: function(req, res) {
/*do somethings*/
},
login: function(req, res){
/*do somethings*/
},
logout: function(req, res){
/*do somethings*/
}
};
There must be a better way to implement using express-load with express4, but not yet found.

Error: Failed to lookup view

Iam getting following error if I try to open the localhost:3000 or localhost:3000/login. Can someone help me what is the problem? It is very strange. Many thanks
My Code
var express = require('express');
var crypto = require('crypto');
var bodyParser = require('body-parser');
var flash = require('connect-flash');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var path = require ('path');
var port = process.env.PORT || 3000;
var MongoClient = require('mongodb').MongoClient,
ObjectID = require('mongodb').ObjectID,
url = require('url');
var db;
//var mongo;
//var collection;
//dataExt = require('./routes/serverExtend');
// setup middleware
var app = express();
app.use(bodyParser());
app.use(flash());
app.use(cookieParser('secret'));
app.use(session({cookie: { secret: 'keyboard cat', maxAge: 60000 }}));
app.use(express.static(__dirname + 'public')); //setup static public directory
app.set('views', __dirname + 'testapp/views'); //optional since express defaults to CWD/views
app.set('view engine', 'ejs');
// Start server
app.listen(port);
console.log('App started on port ' + port);
// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/testDB", function(err, database) { //"mongodb://localhost:27017/test"
if(err) throw err;
db = database;
});
app.get('/', function(req, res) {
res.render('index.ejs', { message: req.flash('signupMessage')}); // load the index.ejs file
app.get('/login', function(req, res) {
res.render('login.ejs');
});
Here is my package.json
{
"name": "NodejsStarterApp",
"version": "0.0.1",
"description": "A sample nodejs"
"dependencies": {
"express" : "~4.0.0",
"ejs" : "*",
"mongodb":"*",
"connect-flash" : "~0.1.1",
"morgan": "~1.0.0",
"body-parser": "*",
"cookie-parser": "~1.0.0",
"express-session": "~1.0.0"
},
"engines": {
"node": "0.10.26"
},
"repository": {}
}
Here is the problem.
change this line
app.set('views', __dirname + 'testapp/views');
to
app.set('views', __dirname + 'views');
Because when you say just __dirname + 'views' it looks in {appName}/views/fileToRender. But because you are saying __dirname + 'testapp/views' it will search {appName}/testapp/views/fileToRender.

login persistence in browser req.user (node.js, mean.io)

I'm following with tutorial "Building AngularJS and Node.js Apps with the MEAN Stack", and just stuck with
"04. Persisting Login between Page Refreshes.mp4"
As said in this part of tutorial, some middleware function must console.log req.user object. But it is not happening.
package.json's dependencies:
"body-parser": "~1.0.2",
"cookie-parser": "^1.1.0",
"express": "~4.1.2",
"express-session": "^1.1.0",
"jade": "~1.3.1",
"mongoose": "~3.8.9",
"morgan": "~1.0.1",
"passport": "^0.2.0",
"passport-local": "^1.0.0",
"serve-favicon": "~2.0.0",
"stylus": "~0.44.0"
server.js (full)
'use strict';
var express = require('express');
var env = process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var app = express();
var mongoose = require('mongoose');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var config = require('./server/config/config')[env];
require('./server/config/express')(app, config);
require('./server/config/mongoose')(config);
var User = mongoose.model('User');
passport.use(new LocalStrategy(function(username, password, done) {
User.findOne({username: username}).exec(function(error, user) {
if(error) { return done(error); }
if(user && user.authenticate(password)) {
return done(null, user);
} else {
return done(null, false);
}
});
}));
// !!!! this middleware must console.log req.user, but it is always undefined.
// req.session displays and seems like correct
app.use(function(req, res, next) {
// console.log(req.session);
console.log(req.user);
next();
});
passport.serializeUser(function(user, done) {
if(user) {
done(null, user._id);
}
});
passport.deserializeUser(function(id, done) {
User.findOne({_id: id}).exec(function(error, user) {
if(!user) {
return done(null, false);
}
return done(null, user);
});
});
require('./server/config/routes')(app);
app.listen(config.port);
console.log('listening on port ' + config.port + '...');
express.js (full)
'use strict';
var express = require('express');
var stylus = require('stylus');
var logger = require('morgan');
var body_parser = require('body-parser');
var cookie_parser = require('cookie-parser');
var session = require('express-session');
var passport = require('passport');
var favicon = require('serve-favicon');
module.exports = function(app, config) {
app.set('views', config.root_path + '/server/views');
app.set('view engine', 'jade');
function compile(str, path){
return stylus(str).set('filename', path);
}
app.use(stylus.middleware({
src: config.root_path + '/public',
compile: compile
}));
app.use(logger());
app.use(cookie_parser('multi vision unicorns'));
app.use(session({ secret: 'keyboard cat', key: 'sid', cookie: { secure: true }}));
app.use(body_parser());
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(config.root_path + '/public'));
app.use(favicon(config.root_path + '/public/favicon.ico'));
// development
// = = = = = = = = = = = =
if ('development' === app.get('env')) {
app.locals.pretty = true;
}
};
Right now I can log in using passport module via POST, but after refreshing page, state of login does not persist, and I have to log in again. In tutorial said, that this middleware
app.use(function(req, res, next) {
console.log(req.user);
next();
});
must console.log user object even after refreshing page. But in does not.
The ending goal of lesson, is to persist state of login for browser, so that field 'username', 'password', button 'Sign In' were hidden after logging in, and stayed hidden after refreshing. But seems like server does not persist state of login too. May be problem with session, may be something else.
Please help..
= = = = = = = =
Problem solved, - had to remove "cookie: { secure: true }" part from:
"app.use(session({ secret: 'secret', key: 'sid', cookie: { secure: true }}));" as it works only with https
I think you need to store the session. (Example in CoffeeScript.)
session = require 'express-session'
mongoStore = (require 'connect-mongo') session
app.use session
secret: 'super secret'
store: new mongoStore(db: 'session')
cookie:
secure: true
maxAge: 2592000000

using dustjs-helpers contextDump with expressjs

I am using dustjs as the templating engine in an express app, and was wondering if anyone has successfully used the contextDump helper in a server side template in express? I followed the consolidate example for integrating dust with express, and it is working well. The helpers are there, as I'm able to do a simple test with the eq helper and it works. but when trying to dump the context I'm not seeing it anywhere. here is a sample template:
{>layout/}
{<content}
<ul>
{#users}
<li>{username} - Create New Task
<ul>
{#user.tasks}
<li>{title} - Delete Task |
Update Task</li>
{/user.tasks}
</ul>
</li>
{/users}
</ul>
{/content}
{#contextDump to="console"/}
Here's my app.js:
var express = require('express');
var app = express();
var http = require('http');
var path = require('path');
var db = require ('./models');
var dust = require('dustjs-linkedin');
var cons = require('consolidate');
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', path.join(__dirname, 'views'));
app.set('models', db);
//configure dust
app.set('view engine', 'dust');
app.set('template_engine', 'dust');
app.engine('dust', cons.dust);
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.json());
app.use(express.urlencoded());
app.use(express.methodOverride());
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// development only
if ('development' == app.get('env')) {
app.use(express.errorHandler());
}
//require routes
require('./routes')(app);
require('./routes/user')(app);
require('./routes/task')(app);
db
.sequelize
.sync({ force: true })
.complete(function (err) {
if (err) {
throw err;
} else {
http.createServer(app).listen(app.get('port'), function(){
console.log('Express server listening on port ' + app.get('port'));
});
}
});
and here's package.json:
{
"name": "application-name",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node app.js"
},
"dependencies": {
"express": "3.4.6",
"sequelize": "~2.0.0-beta.5",
"mysql": "~2.0.0-rc2",
"lodash": "~2.4.1",
"async": "~0.2.9",
"dustjs-linkedin": "~2.2.2",
"dustjs-helpers": "~1.1.1",
"consolidate": "~0.10.0"
}
}
Thanks!

Resources