Reputation: 139
I am not getting the output, this is basically a simple login form, i am getting an error when I submit email id and password in the form.
const express = require('express');
const path = require('path');
const bodyparser = require("body-parser");
const session = require('express-session');
const {v4:uuidv4} = require('uuid');
const router = require('./router');
const app = express();
const port = process.env.PORT||3000;
app.use(bodyparser.urlencoded({extends: true}));
app.use(bodyparser.json());
app.set('view engine','ejs');
//load static assets
app.use('/static', express.static(path.join(__dirname,'public')))
app.use('/static', express.static(path.join(__dirname, 'public/assets')))
app.use(session({
secret:'uuidv4()',
resave:false,
saveUninitialized:true
}))
app.use('/route', router);
//home route
app.get('/',(req,res)=>{
res.render('base',{title:'Login System'});
})
app.listen(port, ()=>{console.log('Server started at port 3000')});
In browser:
TypeError: Cannot read property 'email' of undefined
In command terminal:
body-parser deprecated undefined extended: provide extended option server.js Server started at port 3000
Router Code:
const e = require('express');
var express = require('express');
var router = express.Router();
const credentials = {
email:'[email protected]',
password:'admin123'
}
router.post('/login', (res, req)=>{
if(req.body.email == credentials.email && req.body.password == credentials.password)
{
req.session.user=req.body.email;
//res.redirect('/dashboard');
res.end('Login sucessful!');
}
else
{
res.end('Invalid username or password');
}
})
module.exports=router;
This is the form Code that i used:
<%-include('header')-%>
<div class="text-center center-div" id="login">
<div class="container w-25 border py-5">
<div class="title">
<h2 class="font-weight-bold">Login System</h2>
<span>Login for existing users</span><br><br><br>
</div>
<form action="/route/login" method="POST" class="pt-3">
<div class="frm-group">
<input
type="email"
class="form-control"
placeholder="email"
name="email"
/>
<small class="form-text text-muted"
>Register email address</small
>
</div>
<br>
<div class="form-group">
<input
type="password"
class="form-control"
name="password"
placeholder="password"
/>
</div>
<br>
<button type="submit" class="btn btn-success rounded-pill">Submit</button>
</form>
</div>
</div>
<%-include('footer')-%>
Upvotes: 0
Views: 1662
Reputation: 1036
To fix your undefined issue Replace extends
with extended
and to get rid of the deprecation warning use app.use(express.json());
and app.use(express.urlencoded({ extended: true }));
as you can see in express github line 15 and 78 that body-parser is part of express
Upvotes: 5