'router.get' vs 'router.route.get' in express.js - javascript

I have just started learning MEAN stack and going through tutorial in which I have files, api.js and auth.js.
In api.js I have below route structure,
var express = require('express');
var router = express.Router();
//Used for routes that must be authenticated.
function isAuthenticated (req, res, next) {
// if user is authenticated in the session, call the next() to call the next request handler
// Passport adds this method to request object. A middleware is allowed to add properties to
// request and response objects
//allow all get request methods
if(req.method === "GET"){
console.log('in console');
return next();
}
if (req.isAuthenticated()){
return next();
}
// if the user is not authenticated then redirect him to the login page
return res.redirect('/#login');
};
//Register the authentication middleware
router.use('/posts', isAuthenticated);
router.route('/posts')
.get(function(req,res){
res.send({message:'TODO: return all posts'});
})
.post(function(req,res){
res.send({message:'TODO: create new post'});
});
router.route('/posts/:id')
.get(function(req,res){
res.send({message:'TODO: return post with ID ' + req.params.id});
})
.put(function(req,res){
res.send({message:'TODO: modify post with ID ' + req.params.id});
})
.delete(function(req,res){
res.send({message:'TODO: delete post with ID ' + req.params.id});
});
module.exports = router;
In auth.js I have below route structure,
var express = require('express');
var router = express.Router();
module.exports = function(passport){
//sends successful login state back to angular
router.get('/success', function(req, res){
res.send({state: 'success', user: req.user ? req.user : null});
});
//sends failure login state back to angular
router.get('/failure', function(req, res){
res.send({state: 'failure', user: null, message: "Invalid username or password"});
});
//log in
router.post('/login', passport.authenticate('login', {
successRedirect: '/auth/success',
failureRedirect: '/auth/failure'
}));
//sign up
router.post('/signup', passport.authenticate('signup', {
successRedirect: '/auth/success',
failureRedirect: '/auth/failure'
}));
//log out
router.get('/signout', function(req, res) {
req.logout();
res.redirect('/');
});
return router;
}
Above code works fine but whenever I try to rewrite code of api.js like auth.js structure below,
module.exports = function(){
router.get('/posts',function(req,res)
{
res.send({message:'TODO: return all posts'});
});
router.post('/posts',function(req,res)
{
res.send({message:'TODO: add new post'});
});
router.get('/posts/:id',function(req,res)
{
res.send({message:'TODO: return post with ID ' + req.params.id});
});
router.put('/posts/:id',function(req,res)
{
res.send({message:'TODO: edit post with ID ' + req.params.id});
});
router.delete('/posts/:id',function(req,res)
{
res.send({message:'TODO: delete post with ID ' + req.params.id});
});
return router;
}
This doesn't work. Below is the screen shot of the node cmd prompt whenever I make any post or get request. Am I rewriting code in wrong manner ?

You're exporting a function vs. a Router instance in api.js now. If you didn't change the file that uses api.js accordingly, you will be mounting a function instead of a Router.
So for the new api.js, your parent file will need to be doing something like:
var apiRoutes = require('./api')();
app.use('/api', apiRoutes);
instead of something like:
var apiRoutes = require('./api');
app.use('/api', apiRoutes);

Related

'app.use() requires a middleware function' - Passport and Node Error

I'm trying to add local authentication to my node.js app. After following the tutorial on: https://scotch.io/tutorials/easy-node-authentication-setup-and-local I have run into error: "TypeError: app.use() requires a middleware function".
I think this is related to the app.use(indexRoutes) in my app.js file but I'm not sure how to fix this? Any help would be greatly appreciated.
Here is my code so far:
app.js:
var express = require('express'),
session = require("express-session"),
bodyParser = require('body-parser'),
app = express().use(bodyParser.json()), //creates express http server
passport = require('passport'),
sanitizer = require('express-sanitizer'),
mongoose = require("mongoose"),
cookieParser = require('cookieparser'),
dotnev = require('dotenv').config(),
https = require('https'),
flash = require('connect-flash'),
fs = require('fs'),
config = require('./config/config'),
_ = require("underscore");
require('./config/passport')(passport);
var indexRoutes = require("./routes/index")(app, passport);
app.use(bodyParser.urlencoded({extended: true}));
app.use(sanitizer());
app.use(express.static(__dirname + "/public")); //tells express to serve the contents of the public directory
app.use(cookieParser);
//no longer need to specify that the view files are .ejs
app.set("view engine", "ejs");
app.use(session({
secret: "akjjkjnisaiuu8998323jdkadsih892rhoisdfasl",
resave: true,
saveUninitialized: true,
cookie: {
maxAge: 1200000
}
}));
app.use(passport.initialize);
app.use(passport.session());
app.use(flash());
app.use(indexRoutes);
mongoose.connect(process.env.MONGOLAB_URL, { useNewUrlParser: true });
index.js:
var express = require("express"),
router = express.Router(),
_ = require("underscore"),
User = require("../models/user"),
auth = require("../routes/auth"),
config = require('../config/config'),
freshbooks = require("../modules/freshbooks"),
quickbooks = require("../modules/quickbooks");
module.exports = function(router, passport){
//------------------------------------//
//***------------ROUTES------------***//
//------------------------------------//
router.get("/", function(req, res) {
res.render("landing");
});
router.get("/register", auth.optional, function(req, res) {
res.render("register");
});
router.get("/login", auth.optional, function(req, res) {
res.render("login");
});
router.get("/admin", isLoggedIn, function(req, res) {
res.render('admin', {
user : req.user // get the user out of session and pass to template
});
});
//------------------------------------//
//***-------------AUTH-------------***//
//------------------------------------//
router.post('/register', passport.authenticate('local-signup', {
successRedirect : '/admin', // redirect to the secure profile section
failureRedirect : '/register', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
router.post('/login', passport.authenticate('local-login', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}));
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/');
}
router.get('/logout', function(req, res) {
req.logout();
res.redirect('/');
});
}
Thanks in advance for any help!
In app.js
var { registerIndexRoutes } = require("./routes/index");
var indexRoutes = registerIndexRoutes(router, passport);
app.use(indexRoutes);
and index.js
const registerIndexRoutes = function(router, passport){
//------------------------------------//
//***------------ROUTES------------***//
//------------------------------------//
router.get("/", function(req, res) {
res.render("landing");
});
return router;//important;
}
module.exports = { registerIndexRoutes };
It will help you.
You're getting that error because, this function, module.exports = function(router, passport){, is not a valid middleware.
From express docs: Middleware functions are functions that have access to the request object (req), the response object (res), and the next function in the application’s request-response cycle.
The syntax goes:
function(req, res, next) {
// Implement the middleware function based on the options object
next()
}
req: HTTP response argument to the middleware function, called "res" by convention.
res: HTTP request argument to the middleware function, called "req" by convention.
next: Callback argument to the middleware function, called "next" by convention.
When you invoked this function, function(router, passport){:
var indexRoutes = require("./routes/index")(app, passport);
indexRoutes, contains the return value of function(router, passport) which does not return a middleware function (It returns undefined).
Two ways you can solve the issue:
Change your import:
// remove this line and put the below instead
app.use(cookieParser);
// and modify and move your import
require("./routes/index")(app, passport);
Use Express router: Change your index.js like
// note your exports it's not a function, you're exporting this **router** object -> router = express.Router(),
module.exports = router;
//------------------------------------//
//***------------ROUTES------------***//
//------------------------------------//
router.get("/", function(req, res) {
res.render("landing");
});
router.get("/register", auth.optional, function(req, res) {
res.render("register");
});
...
And change your import to:
var indexRoutes = require("./routes/index");

TypeError: Cannot read property 'apply' of undefined for Express framework

app.js
var app = express();
app.listen(PORT, () => console.log(`Listening on ${ PORT }`));
// all environments
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
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')));
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: false,
// cookie: {
// maxAge: 365 * 24 * 60 * 60 * 1000,
// path : '/'
// }
}));
app.use('/portal/admin', adminRouter);
app.use('/portal/merchant', indexRouter);
app.use('/users', usersRouter);
app.use('/api/v1/users',apiRouter);
app.use('/api/v1/users',customerInstallmentAPIRouter);
app.use('/api/v1/payment',paymentMethodAPIRouter);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization, Content-Type');
res.setHeader('Access-Control-Allow-Credentials', true);
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');
});
app.get('/portal/merchant',indexRouter); //call to index site
//login
app.get('/login', usersRouter); // call to login site
app.post('/login',usersRouter); // post to /users/login site
//logout
app.get('/home/logout',usersRouter);
//signup
app.get('/signup', usersRouter); // call to /users/signup site
app.post('/signup',usersRouter); //call to /post/signup
//dashboard
app.get('/home/dashboard',usersRouter);
//profile
app.get('/home/profile',usersRouter);
db.sequelize
.authenticate()
.then(() => {
console.log('Connection has been established successfully.');
})
.catch(err => {
console.error('Unable to connect to the database:', err);
});
//run scheduler to check due date
//cronJob.dueDateCronJob();
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
require('./routes/adminportal/home.js')(app,passport);
module.exports = app;
It seems that the error happens at require('./routes/adminportal/home.js')(app,passport);
passport.js
// config/passport.js
// load all the things we need
var LocalStrategy = require('passport-local').Strategy;
// load up the user model
var User = require('../models/admin.js');
// expose this function to our app using module.exports
module.exports = function(passport) {
// =========================================================================
// passport session setup ==================================================
// =========================================================================
// required for persistent login sessions
// passport needs ability to serialize and unserialize users out of session
// used to serialize the user for the session
passport.serializeUser(function(user, done) {
done(null, user.id);
});
// used to deserialize the user
passport.deserializeUser(function(id, done) {
User.findById(id, function(err, user) {
done(err, user);
});
});
// =========================================================================
// LOCAL LOGIN =============================================================
// =========================================================================
// we are using named strategies since we have one for login and one for signup
// by default, if there was no name, it would just be called 'local'
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email_address',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) { // callback with email and password from our form
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' : email }, function(err, user) {
// if there are any errors, return the error before anything else
if (err)
return done(err);
// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash
// if the user is found but the password is wrong
if (!user.validPassword(password))
return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata
// all is well, return successful user
return done(null, user);
});
}));
};
home.js
var express = require('express');
var router = express.Router();
var db = require('../sequelizeDB.js');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
/* GET home page. */
router.get('/', function(req, res, next) {
if(req.session.userId != null){
message = '';
//res.render('dashboard',{message:message});
res.redirect("adminportal/home.ejs");
}else{
var message = '';
var sess = req.session;
res.render('adminportal/login.ejs',{message: message});
}
});
router.post('/login',passport.authenticate('local-login', {
successRedirect : '/listOfCustomers', // redirect to the secure profile section
failureRedirect : '/', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}), function(req, res, next) {
var message = '';
var sess = req.session;
if(req.method === "POST"){
var post = req.body;
var name= post.user_name;
var pass= post.password;
} else {
res.render('adminportal/login.ejs',{message: message});
}
});
function isLoggedIn(req, res, next) {
// if user is authenticated in the session, carry on
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('adminportal/login.ejs');
}
router.get('/listOfCustomers',isLoggedIn, function(req, res, next) {
if(req.method === "GET"){
db.customers.findAll().then(customers =>{
res.render('adminportal/listOfCustomers.ejs',{data:customers});
})
}
});
module.exports = router;
Am I doing it wrongly ? I am following a tutorial on this website: https://scotch.io/tutorials/easy-node-authentication-setup-and-local
I am trying to do authentication on my website by using passport.js. Been struggling for hours to solve this. Any help will be appreciated. Thanks.
The home.js file you show exports a router. A router is not something you import like this:
require('./routes/adminportal/home.js')(app,passport);
If you look at the code for /app/routes.js in the tutorial you pointed to, the file that works with that type of import is shown there and it has an export like this:
module.exports = function(app, passport) { ... }
So, you appear to have files mixed up when trying to follow that demo. You're exporting a router, but attempting to call a function that should have been exported like the line above.
Since I can't see the overall lay of the land in your code, all I can tell you is that when you export a router, you then use it like this this:
app.use('/someOptionalPath', require('./routes/adminportal/home.js'));
or just:
app.use(require('./routes/adminportal/home.js'));
depending upon exactly what you're trying to do. That's how you hook a router into your web server.
I founded the following error
/www/wwwroot/domain.com/node_modules/express/lib/router/index.js:646
return fn.apply(this, arguments);
^
TypeError: Cannot read property 'apply' of undefined
at Immediate. (/www/wwwroot/domain.com/node_modules/express/lib/router/index.js:646:15)
at processImmediate (internal/timers.js:466:21)
And solved by replacing return fn.apply(this, arguments); with
return (fn?.apply(this, arguments)) ? fn.apply(this, arguments) : ''; in
(/www/wwwroot/domain.com/node_modules/express/lib/router/index.js:646:15)
Hope It saves some one

How to pass arguments into an outside module?

I have the following function that I placed inside a separate js file.
I am trying to use it inside another javascript file that requires passport.js, and I would like to call it using app.use to further modularize my application
var express = require('express');
var router = express.Router();
/* GET welcome page. */
router.get('/home', isLoggedIn, function(req, res, next) {
res.render('home', {
title: 'title',
user : req.user
});
});
// route middleware to make sure
function isLoggedIn(req, res, next) {
// if user is authenticated in the session
if (req.isAuthenticated())
return next();
// if they aren't redirect them to the home page
res.redirect('/');
}
module.exports = router;
The reason I created it is so I reduce redundancy and not use the following code each time:
app.get('/home', isLoggedIn, function(req, res) {
res.render('home.ejs', {
user : req.user // get the user out of session and pass to template
});
});
However I can't seem to get it to work. is authenticated is undefined although it is in the same folder, and it gives me an error 404 not found when I issue a get. How I can keep it in an external file and still use it? should I also pass it the user argument from where I am calling it?
var express = require('express');
var router = express.Router();
module.exports = function (app, passport){
router.get('/home', isLoggedIn, function(req, res, next) {
res.render('home', {
title: 'title',
user : req.user
});
});
return router;
};
function isLoggedIn(req, res, next) {
// if user is logged in -
if (req.isAuthenticated())
return next();
// if they aren't redirect them to home
res.redirect('/');
}

Access session object in Express middleware

This is my Express middleware stack:
var server = express()
.use(express.cookieParser())
.use(express.session({secret: 'Secret'}))
.use(express.bodyParser())
.use(function printSession(req, res, next) {
console.log(req.session.user);
next();
})
.use(express.static('./../'));
and here are two routes:
server.post('/setSession', function (req, res) {
req.session.user = 'admin';
}
server.post('/getSession', function (req, res) {
console.log(req.session.user);
}
Now the session management in the route handlers work find. I can set session.user and it will persist for the subsequent requests in the same session, as confirmed by getSession. However, the middleware function printSession always prints undefined.
How can I access the populated session object in the middleware?
This program works fine. Before I access /setSession, the middleware prints after session: undefined. Once I GET /setSession, it prints after session: admin. As long as the browser you are testing with (not curl) stores and sends the session cookies, this will work as expected.
var express = require('express');
var server = express();
server.use(express.cookieParser());
server.use(express.session({secret: 'SEKRET'}));
server.use(function (q,r,n) {console.log('after session:', q.session.user);n();});
server.get('/', function (q,r,n) {r.send("you got slashed");});
server.get('/setSession', function (req, res) {
console.log("logging admin in via /setSession");
req.session.user = 'admin';
res.send("admin logged in");
});
server.listen(3000);
There must be something wrong with your settings. The following example, that is very similar to your code but uses GET instead POST, works fine for me
app.configure(function(){
// ...
app.use(express.cookieParser('your secret here'));
app.use(express.session());
app.use(function(req, res, next) {
console.log(req.session.user + ' from middleware');
next();
});
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
});
and
app.get('/getSession', function(req, res) {
console.log(req.session.user);
res.send('awesome');
});
app.get('/setSession', function(req, res) {
req.session.user = 'admin';
res.send('done');
});
Now when you do the following everything works as expected
GET /getSession => undefined from middleware, undefined
GET /setSession => undefined from middleware
GET /getSession => admin from middleware, admin

node.js express routing db dependent

What i'm trying to do:
If the url exists in the db use static page template, if not, display specific page template. Can't seem to figure out, how too...
My app.js file
app.get('*', function(req, res){
var currenturl = req.url;
console.log('URL IS: ' + my_path)
if (!!db.get(my_path) )
{
//If it does exist in db
console.log('Does exist');
res.render('index', { thetitle: 'Express', title: db.get(currenturl).title, content: db.get(currenturl).content });
}else{
//If it doesn't exist in db
redirect to other sites
Like:
if you go to "/page" it will run this => app.get('/page', routes.index)
or "/users" will run => app.get('/users', routes.users)
}
});
You have to create your own simple middleware. Just make sure you put it above the express.router
app.use(function(req, res, next){
if (!!db.get(my_path)) {
// render your site from db
} else {
// call next() to continue with your normal routes
next();
}
});
app.get('/existsInDB', function(req, res) {
// should be intercepted by the middleware
})
app.get('/page', function(req, res) {
// should not be intercepted
res.render('page')
})
using express is easy. you can use the redirect function:
if (url_exists) res.render('index');
else res.redirect('/foo/bar');

Categories