Use Node Express session between different file in node - javascript

I saved email in session in my login.js
router.get('/signin',function(req,res,next) {
res.send(req.session.email);//here i can get email from session as well
console.log("email inside ./signin in login.js "+req.session.email);
});
router.post('/login', function(req, res) {
req.session.email=req.body.email; //get email from form body
console.log(req.session.email); // here i can get my email in console
res.redirect('/') //want to redirect to home
});
I want to access email from session in my index.js
var session;
router.get('/', function(req, res, next) {
session=req.session;
console.log("your mail is"+session.email);
});
And this is app.js config
var
express = require('express');
var app = express();
var path = require('path');
var connection=require('./models/connection');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session=require('express-session');
var index = require('./routes/index');
var users = require('./routes/users');
var login=require('./routes/login');
var debug = require("debug");
var clc = require('cli-color');
var router=express.Router();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(favicon(path.join(__dirname, 'public/images/', 'favicon.gif')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser("secretkey"));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/assets', express.static(__dirname + '/assets'));
app.use(session({secret: 'secretkey',saveUninitialized: true,resave: true}));
app.get('/login',login);
app.post('/login',login);
app.get('/signin',login);
app.get('/',index);
module.exports = app;
Want some thing like this we can do in php
<!-- first page -->
<?php
session_start();
$_SESSION['myvar'] = 'hello';
?>
<!-- second page -->
<?php
session_start();
echo $_SESSION['myvar']; // it will print hello
?>
I tried these and some more answer but none of them solve my problem
NodeJS express-session req.session is undefined
req.session is undefined in Node?
How to use req.session data in an other javascript file expressjs

So, I tried your code as-is as best I could (copied and pasted from yours, added files that were needed that you didn't post here, e.g. a server.js file that actually starts listening on a port), and it worked as-is with just one change.
Your index.js file does not ever actually send anything back to the client. Note your code:
var session;
router.get('/', function(req, res, next) {
session=req.session;
console.log("your mail is"+session.email);
});
There's no res.send or res.render called, so it's going to just hang. It has nothing to do with the session value, though, the console.log statement still prints out the correct email value that was posted before.
Try:
var session;
router.get('/', function(req, res, next) {
session=req.session;
console.log("your mail is"+session.email);
res.send(session.email);
});

Related

Root not working for admin pannel in node js

I am tring to give 2 roots one for admin and other for user.
To access the admin pannel http://localhost:3000/admin
To access to user page http://localhost:3000.
But the problem is when an option is selected from the menu of admin pannel it will not take to that page , and shows a 404 error.[if i click option which have an href add-faq it must show like http://localhost:3000/admin/add-faq. but it is showing http://localhost:3000/add-faq and giving 404 error]
But the user page had no problems.
I am using node js,Express,Hbs.
All files are named properly and no files missing..
If the http://localhost:3000/admin/add-faq is manually given the stylesheets are not loading (404)
GITHUB :https://github.com/bimalboby/clevercode
PLEASE HELP AND THANKS IN ADVANCE😊
My App.js file:
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var userRouter = require('./routes/user');
var adminRouter = require('./routes/admin');
var hbs = require('express-handlebars')
var app = express();
var db=require('./config/connection')
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.engine('hbs',hbs({extname:'hbs',defaultLayout:'layout',layoutDir:__dirname+'/views/layouts',partialsDir:__dirname+'/views/partials'}))
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
db.connect((err)=>{
if(err) console.log('connection failed'+err);
else console.log('connected to database');
})
app.use('/', userRouter);
app.use('/admin',adminRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;```
**My admin.js:**
var express = require('express');
var router = express.Router();
/* GET users listing. */
router.get('/', function(req, res, next) {
res.render('adminintro',{admin:true})
});
router.get('/add-prices', (req,res)=>{
res.render('faqadmin',{admin:true})
});
router.get('/add-faq',(req,res)=>{
res.render('faqadmin',{admin:true})
})
module.exports = router;
**my user.js:**
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/', function(req, res, next) {
res.render('index',{admin:false,indexpage:true});
});
router.get('/faq',(req,res)=>{
res.render('faq',{indexpage:true})
});
router.get('/web-design-pricing',(req,res)=>{
res.render('price',{indexpage:true})
});
router.get('/e-commerce-pricing',(req,res)=>{
res.render('price',{indexpage:true})
});
router.get('/seo-pricing',(req,res)=>{
res.render('price',{indexpage:true})
});
module.exports = router;
Try adding the complete end point like below in /views/partials/admin-header.hbs file like below,
<li>FAQ</li>
Your user routes are with the endpoint '/' in server.js.So its working,where as admin need to be linked like /admin/add-faq

How can I make session variables accessible to sub application

I am building an api that will interface with the MongoDB database and have mounted it as a subapplication. I have defined a session variable in my server controller.
However, any time that the server files need to talk to the api files the session variables are never passed off.
Heres the app.js file
//app.js file
'use strict';
process.env.NODE_ENV = 'development';
var express = require('express');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var MongoStore = require('connect-mongo')(session);
var flash = require('connect-flash');
var helmet = require('helmet');
var app = express();
app.use(helmet());
var port = process.env.PORT || 3000;
mongoose.connect("mongodb://localhost:27017/striv4");
var db = mongoose.connection;
// mongo error
db.on('error', console.error.bind(console, 'connection error:'));
app.use(session({
secret: 'secret',
resave: true,
saveUninitialized: false,
store: new MongoStore({
mongooseConnection: db
})
}));
app.use(flash());
// make user ID available in templates
app.use(function (req, res, next) {
res.locals.currentUser = {
username:req.session.username,
id: req.session.userId
};
next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser('secreter'));
app.use(logger('dev'));
var api = require('./app_api/routes/index');
var serverRoutes = require('./server/routes/index');
//static file middleware
app.use(express.static(__dirname + '/public'));
app.set('views',__dirname +'/server/views');
app.set('view engine','pug');
app.use('/',serverRoutes);
app.use('/api',api);
//custom error handler
app.use(function(error, req, res, next) {
res.status(error.status || 500);
res.send('Error: '+error.message);
});
app.listen(port);
console.log('Listening on port: '+port);
You've got the whole program listed so there is more than one way for this to have gone wrong. Here are my suggestions to fix this:
Check the version of express-session you've installed. (Just run npm ls in the terminal and in your root Node app folder where you're package.json file is). If it's equal to or greater than v1.5.0, you don't need the cookie-parser for sessions anymore. Comment out the app.use line for the cookie parser and see if this works.
If you still need cookie parser for some other reason, you should use the same secret for sessions and the cookie parser. In your code, you've set two different values for secret.
I've seen that the other big failure for sessions occurs if the session store is not correctly connected to your Node app. Cross-check that the database is available and working. In my experience, Express sessions will fail silently if it can't get to the DB.
Hope this helps.

Get undefined in req.body.id (body-parser and express 4) Nodejs

app.js:
var express = require('express');
var path = require('path');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var person = require('./routes/person');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/person', person);
module.exports = app;
routes/person.js:
var express = require('express');
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
var router = express.Router();
/* GET page. */
router.get('/', function (req, res) {
res.render('person', {
message: 'Person works'
});
});
router.post('/', urlencodedParser, function (req, res) {
res.send('Thank you!');
console.log(req.body.firstname);
console.log(req.body.lastname);
});
views/person.pug:
extends layout
block content
h1 Welcome #{id}
p= message
br
h2= qstr
br
form(method='post', action='/person')
label First name
input#firstname(type='text')
br
label Last name
input#lastname(type='text')
input(type='submit', value='Submit')
Questions:
1) Is it necessary in every route to add?:
var bodyParser = require('body-parser');
var urlencodedParser = bodyParser.urlencoded({ extended: false });
2) Why do I get this:
1.You don't need to use body-parser in every route. Body-parser is a middleware which is used to obtain data from application/x-www-urlencoded content type. So if you're sure sure that data you will get in your body is not x-www-urlencoded type, you don't need to use it.
2.Please check if you are passing the data in post request. You can use chrome extension postman to form any kind of query.

Creating a working form in ExpressJS

I am new to NodeJS and ExpressJS development (a week old or so). I am trying to create a web application in NodeJS using ExpressJS framework. One of the things I am trying to do is build a registration form for my app. I have installed body-parser middleware using the npm to read form data.
I am using HoganJS as my template framework. I have a page in my views folder named register.hjs. This page has a form
<form method="post">
<input type="text" name="name">
<input type="text" name="age">
<input type="submit">
</form>
I am struggling with these two issues:
How to read form values in the .js
How to redirect a user to a different page once form is submitted.
This is what I am trying to do (it might be incorrect though).
In my app.js
//get
app.get('/register', routes.register);
//post
app.post('/welcome', routes.welcome);
In my index.js
/* GET about page.*/
exports.register = function(req, res) {
res.render('register');
};
/*POST registered user*/
exports.welcome = function(req, res) {
// pull the form variables off the request body
var name = req.body.name;
var age = req.body.age;
//just to make sure the information was read
console.log(name);
console.log(age);
res.render('welcome');
};
I am pretty sure I am missing something cause when I run my server it gives an error saying :
Error: Route.get() requires callback functions but got a [object Undefined]
What am I doing wrong? Is there any elegant way to read form data and redirect users to different pages?
Thanks.
--------------------------- Update ----------------------------
Here is the rest of the app.js Code
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
//routes to the pages
var routes = require('./routes/index');
var users = require('./routes/users');
var register = require('./routes/register');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views')); //app.set('the name of your view folder', )
app.set('view engine', 'hjs');
//get
app.get('/register', routes.register);
//post
app.post('/welcome', routes.welcome);
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
app.use('/users', users);
app.use('/register', register);
//error handlers
//error handlers
module.exports = app;
For Simple understanding how express works for form understand this code :
After understanding this code , use router and other body parser configuration---
var express = require('express');
/*
* body-parser is a piece of express middleware that
* reads a form's input and stores it as a javascript
* object accessible through `req.body`
*
* 'body-parser' must be installed (via `npm install --save body-parser`)
* For more info see: https://github.com/expressjs/body-parser
*/
var bodyParser = require('body-parser');
// create our app
var app = express();
// instruct the app to use the `bodyParser()` middleware for all routes
app.use(bodyParser());
// A browser's default method is 'GET', so this
// is the route that express uses when we visit
// our site initially.
app.get('/', function(req, res){
// The form's action is '/' and its method is 'POST',
// so the `app.post('/', ...` route will receive the
// result of our form
var html = '<form action="/" method="post">' +
'Enter your name:' +
'<input type="text" name="userName" placeholder="..." />' +
'<br>' +
'<button type="submit">Submit</button>' +
'</form>';
res.send(html);
});
// This route receives the posted form.
// As explained above, usage of 'body-parser' means
// that `req.body` will be filled in with the form elements
app.post('/', function(req, res){
var userName = req.body.userName;
var html = 'Hello: ' + userName + '.<br>' +
'Try again.';
res.send(html);
});
app.listen(80);
app.js:
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hjs');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', routes);
module.exports = app;
index.js:
var express = require('express');
var router = express.Router();
/* GET home page. */
router.get('/register', function(req, res) {
res.render('register');
});
router.post('/welcome', function(req, res) {
console.log(req.body.name);
console.log(req.body.age);
});
module.exports = router;

Node.js express POST 404ing

I've got a small node.js application using the express framework, but for some reason I can't get my application to respond to POST requests. In the server log I simply get "POST / 404 5ms", and I can't figure out why.
EDIT: To clarify - My problem is that app.post doesn't seem to be doing anything
EDIT 2: I somehow managed to fix this last night, but now I can't figure out at what point i fixed it.
Node.js server code:
var express = require('express')
, routes = require('./routes')
, user = require('./routes/user')
, http = require('http')
, path = require('path');
var app = express();
// all environments
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(express.cookieParser('chocolatechip'));
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());
}
//pages
//Landing page
app.get('/', routes.index);
app.post('/test',function(req,res){
console.log(req.body);
res.send("received post");
});
//return list containing users
//app.post('/users', user.list);
//return requested user
app.get('/users/:id', user.get);
//app.post('/users/login', user.login);
//server
http.createServer(app).listen(app.get('port'), function(){
console.log('Server listening on port ' + app.get('port'));
});
On the actual webpage, I've got the following javascript code:
var login = $('#login');
var page = $('#page');
var register = $('#register');
var userField = login.find('.user');
var passField = login.find('.pass');
var confPassField = login.find('.confpass');
var form = $('.logform');
$('#formbutton').on('click',function(){
if(register.hasClass('hidden')){
login.addClass('hidden');
confPassField.val('');
var logDat = JSON.stringify(form.serializeArray);
userField.val('');
passField.val('');
page.html("Login form submitted");
$.post(
form.attr("action"),
{test:"test"},
function(data){
alert("Response: "+data)
}
);
}
If you are posting to / as your log is saying that you are "POST / 404 5ms", you need to change the following line:
app.get('/', routes.index);
to
app.all('/', routes.index);
This will allow a GET or POST to that route. You can also just use app.post() if you are only posting to that route. Hope this helps.
Docs here: http://expressjs.com/api.html#app.all
Make sure that 'form.attr("action")' is getting the proper URL. It seems that your form is posting to the index page rather than to '/test'. Maybe that should be changed to $('form').attr("action")
For me the problem was that I had my
app.post('/test', jsonParser, function (req, res) {
console.log(req);
res.send('Ok');
});
below this part added by express-generator to my app.js
// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});
By changing the order in the file I resolved this problem.

Categories