Express4.10 bodyParser req.body undefined - node.js

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.

Related

I cannot POST using express while following MVC

I'm trying to build a simple to-do list. I keep getting a cannot POST error trying to add a to-do using a form to submit. I'm not sure if I'm linking my files correctly or not while trying to follow MVC. Am I missing a module?
app.js (trimmed down because SO says I have mostly code)
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const exphbs = require("express-handlebars");
const MongoStore = require("connect-mongo");
const connectDB = require("./config/db");
// config.env is where all the global variables are stored.
dotenv.config({ path: "./config/config.env" });
// This call connects to mongodb.
connectDB();
// Initialize app
const app = express();
// Body parser
app.use(express.urlencoded({ extended: false }));
app.use(express.json());
// Handlebars
app.engine(".hbs", exphbs({ extname: ".hbs" }));
app.set("view engine", "hbs");
// Sessions
app.use(
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: false,
store: MongoStore.create({ mongoUrl: process.env.MONGO_URI }),
})
);
// Passport middleware
app.use(passport.initialize());
app.use(passport.session());
// Static folder
app.use(express.static(path.join(__dirname, "public")));
const PORT = process.env.PORT || 3000;
// Routes
app.use("/", require("./routes/index"));
app.use("/auth", require("./routes/auth"));
// app.use("/todo", require("./routes/todo"));
app.listen(
PORT,
console.log(`Server running in ${process.env.NODE_ENV} mode on PORT ${PORT}`)
);
todo.js in Route
const express = require("express");
const router = express.Router();
const todosController = require("../controllers/todos");
// const { ensureAuth, EnsureGuest } = require("../middleware/auth");
router.get("/", todosController.getTodos);
router.post("/addTodo", todosController.addTodo);
todo.js in Controller
const Todo = require("../models/todos");
module.exports = {
addTodo: async (req, res) => {
try {
await Todo.create({
todo: req.body.todoItem,
done: false,
googleId: req.user.googleId,
});
console.log("To do has been added!");
res.redirect("/");
} catch (err) {
console.error(err);
}
},
};
handlebars form
<form class="center" action="/addTodo" method="POST">
<input type="text" placeholder="Add a To Do" name="todo" />
<button type="submit" class="submitButton">
<i class="fa fa-plus-square"></i>
</button>
</form>
and lastly my folder structure

how to load assets in ejs file when you have two different main routes?

i have simple blog express app which i set up of two different main routes, regular routes (login,post,signup etc...) and for admin (add post,edit post etc...) the issue is my assets, css or any other static files does not load in the admin routes only but fortunately works in regular routes. my express setup is as follow:
app.use('/admin/pages', adminPages);
app.use('/', pages);
so after investigating the chrome developer tools for both main routs this is the issue but have no idea how to solve it !
this is when the admin routs does not load bootstrap or css files:
notice all assets is prefixed with unwanted (admin/pages/...) after localhost:3005
but it works here (without the prefix) :
heres my file structure:
full App.js :
const express = require('express'),
app = express(),
path = require('path'),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
pages = require('./routes/pages.js'),
config = require('./config/database'),
expressSession = require('express-session'),
expressValidator = require('express-validator'),
adminPages = require('./routes/admin_pages.js');
mongoose.connect(config.database);
const database = mongoose.connection;
database.on('error', console.error.bind(console, '##error with database:'));
database.once('open', () => {
console.log('Connected To Database successfully');
});
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
// app.use(express.static('public'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.set('trust proxy', 1);
app.use(
expressSession({
secret: 'keyboard cat',
resave: false,
saveUninitialized: true,
cookie: { secure: true }
})
);
app.use(require('connect-flash')());
app.use(function(req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
app.use(
expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.'),
root = namespace.shift(),
formParam = root;
while (namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param: formParam,
msg: msg,
value: value
};
}
})
);
app.use('/admin/pages', adminPages);
app.use('/', pages);
const port = 3005;
app.listen(port, () => {
console.log(`App Listening # localhost:${port}`);
});
add_page.ejs :
<%- include('../_layouts/header') %>
<form>
<input type="text" />
<input type="text" />
<input type="text" />
<button> submit </button>
</form>
<%- include('../_layouts/footer') %>
heres my both main routes:
i've found the answer.
when using two main routes you need to specify each route to its static file so we have got this middleware for the regular route as follow :
app.use('/', express.static('public'));
since you have another route for admin/pages this was missing:
app.use('/admin/pages', express.static('public'));
this fixed the issue :D
In my case I'm using only one main route, so I changed the asset url adding a / at the beginning of the url and it work for me:
<link rel="stylesheet" href="assets/css/style.css">
to
<link rel="stylesheet" href="/assets/css/style.css">
Maybe it can be helpful for someone else, regards!

Node/Express JS "Cannot read property 'username' of undefined"

Before you mark this as a duplicate, please understand that none of the answers I have looked at pertain enough to my code to fix the issue. I am trying to fix an error in a web app that I am making to upload files to a home server from the internet. I am currently working on the basic user authentication system. When I try to submit the login form on the home page, I am redirected to a page with an error, part of which says "Cannot read property 'username' of undefined". This message also appears in console. What is happening and how can I fix it? The error directs me to line eight of the home controller, "username = req.body.username".
App.js
var LocalStrategy = require('passport-local').Strategy;
var flash = require("connect-flash");
var passport = require("passport");
var session = require("express-session");
var expressValidator = require("express-validator");
var mongoose = require("mongoose");
var path = require('path');
var express = require('express');
var cookieParser = require('cookie-parser');
var app = express();
var bodyParser = require('body-parser');
var ejs = require('ejs');
var routes = require('./server/routes');
routes(app);
app.set('port', process.env.PORT || 3000);
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, 'public')));
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(cookieParser());
mongoose.connect("mongodb://localhost:27017/users");
app.listen(app.get('port'), function(){
console.log("server started");
});
var db = mongoose.connection;
app.use(session({
secret: 'yVVma9ga',
saveUninitialized: true,
resave: true
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(expressValidator({
errorFormatter: function(param, msg, value) {
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length) {
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam,
msg : msg,
value : value
};
}
}));
app.use(flash());
app.use(function(req,res,next) {
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash("error");
next();
});
/server/routes.js
var express = require("express");
var home = require('../controllers/home');
var client = require('../controllers/client');
module.exports = function(app) {
var router = express.Router();
app.use('/', router);
router.get('/', home.index);
router.get('/client', client.home);
router.post('/', home.login);
}
controllers/home.js
module.exports = {
index: function(req, res){
res.render("home");
},
login: function(req, res){
var username = req.body.username;
var password = req.body.password;
req.checkBody('username', 'Username is required').notEmpty();
req.checkBody('password', 'Password is required').notEmpty();
var errors = req.validationErrors();
if(errors){
console.log("YES");
}else{
console.log("NO");
};
}
}
/views/home.ejs (partials are just basic html)
<% include partials/header %>
<p>Welcome to your personal server!</p>
<p>Login!</p>
<form method="post" action = "/">
<input placeholder="Username" type="text" name="username">
<input placeholder="Password" type="password" name="password">
<input type="submit" value="Submit">
</form>
<% include partials/footer %>
My bet is that your call to routes is happening before your app.use is calling bodyparser. Try moving your routes below them.
Change
var routes = require('./server/routes');
routes(app);
//...
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
To
var routes = require('./server/routes');
//...
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//...
routes(app);
var routes = require('./server/routes');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
//...
routes(app);
Everything should be inside the route. If not, you will have undefined error.

req.body becomes undefined after passing to res.render

My problem is that req.body becomes undefined when I try to render a page.
My app code:
var https = require('https');
var fs = require('fs');
var mongourl = "mongodb://localhost:27017/auntyinda";
var mongoose = require('mongoose');
fs.readdirSync(__dirname + '/models').forEach(function (filename) {
if (~filename.indexOf('.js')) require(__dirname + "/models/" + filename);
});
//var queries = require('./mongoq/query')
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 expressSession = require('express-session');
var passport = require('passport');
var myPass = require('./security/auth')
var routes = require('./routes/index');
//var users = require('./routes/users');
var app = express();
var server = https.createServer({
cert: fs.readFileSync(__dirname + '/my.crt'),
key: fs.readFileSync(__dirname + '/my.key')
},app).listen(4000);
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
//app.configure(function(){
app.use(logger('dev'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
//app.use(require('connect').bodyParser())
app.use(cookieParser());
app.use(expressSession( {
secret: process.env.SESSION_SECRET || 'sonic12',
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize());
app.use(passport.session());
app.use(express.static(path.join(__dirname, 'public')));
//});
mongoose.connect(mongourl)
//app.use('/', myPass);
app.use('/', routes);
//app.use('/users', users);
//app.locals.appdata = require("./data.json")
My route code:
router.post('/register', function(req, res, next) {
regcheck(req.body.username, req.body.password, req.body.passwordConfirm, function (error) {
if (error) {
console.log(req.body) //OUTPUT IS WHAT IS EXPECTED. NICE JSON FORMAT.
res.render('register', {
title: 'Register',
classname: 'register',
socialIcons: socialIcons,
isAuthenticated: false,
regdata: req.body,
err: error
})
}
})
})
I want the render to use the regdata object to refill data that was good back into the registration form. But for some strange reason it becomes undefined by the time it reaches the render.
My EJS file contains this line at the top:
<% console.log('Error recieved: '); console.log(err); console.log(regdata); %>
But the output of the console.log(regdata) is undefined. What is happening here? The err prints as it should. Thanks in Advance for your help.
Try this,
<pre><%= regdata %></pre>
I think you are missing out a = somewhere.
Consult the docs.

Node.js + express: specify routes

In my understanding, the way to serve views is to do the following:
app.set('view engine', 'ejs'); // or jade or whatever
app.set('views', __dirname + '/views'); // specify where to find the view files e.g. index.ejs
app.get("/", function(req, res) { res.render('index') });
However, when I check the code here https://github.com/jedireza/drywall/ , which is a boilerplate for node user management, I don't see any routes defined in app.js. But it works fine and if I type the url /signup in browser it will render signup.jade.
Which part, or which middleware is doing the magic of routing?
app.js content:
'use strict';
//dependencies
var config = require('./config'),
express = require('express'),
cookieParser = require('cookie-parser'),
bodyParser = require('body-parser'),
session = require('express-session'),
mongoStore = require('connect-mongo')(session),
http = require('http'),
path = require('path'),
passport = require('passport'),
mongoose = require('mongoose'),
helmet = require('helmet'),
csrf = require('csurf');
//create express app
var app = express();
//keep reference to config
app.config = config;
//setup the web server
app.server = http.createServer(app);
//setup mongoose
app.db = mongoose.createConnection(config.mongodb.uri);
app.db.on('error', console.error.bind(console, 'mongoose connection error: '));
app.db.once('open', function () {
//and... we have a data store
});
//config data models
require('./models')(app, mongoose);
//settings
app.disable('x-powered-by');
app.set('port', config.port);
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
//middleware
app.use(require('morgan')('dev'));
app.use(require('compression')());
app.use(require('serve-static')(path.join(__dirname, 'public')));
app.use(require('method-override')());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser(config.cryptoKey));
app.use(session({
resave: true,
saveUninitialized: true,
secret: config.cryptoKey,
store: new mongoStore({ url: config.mongodb.uri })
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(csrf({ cookie: { signed: true } }));
helmet(app);
//response locals
app.use(function(req, res, next) {
res.cookie('_csrfToken', req.csrfToken());
res.locals.user = {};
res.locals.user.defaultReturnUrl = req.user && req.user.defaultReturnUrl();
res.locals.user.username = req.user && req.user.username;
next();
});
//global locals
app.locals.projectName = app.config.projectName;
app.locals.copyrightYear = new Date().getFullYear();
app.locals.copyrightName = app.config.companyName;
app.locals.cacheBreaker = 'br34k-01';
//setup passport
require('./passport')(app, passport);
//setup routes
require('./routes')(app, passport);
//custom (friendly) error handler
app.use(require('./views/http/index').http500);
//setup utilities
app.utility = {};
app.utility.sendmail = require('./util/sendmail');
app.utility.slugify = require('./util/slugify');
app.utility.workflow = require('./util/workflow');
//listen up
app.server.listen(app.config.port, function(){
//and... we're live
});
The routes are being added here:
//setup routes
require('./routes')(app, passport);

Resources