0

I am trying to make login system using html,nodejs,express.

I have created a form and gave the input tag name as myemail, I used this name just to display email in console.But I am getting this error.

I have checked all the answers provided in the similar questions, but none of them worked for me. app.js

const express = require('express');
const path = require('path');
const session = require('express-session');
const bodyparser = require('body-parser');
const app = express();

const port = process.env.PORT || 5000;

const router = require('./router');
const bodyParser = require('body-parser');

app.set('view engine','ejs');
app.use('/static',express.static(path.join(__dirname,'public')));
app.use('/assets',express.static(path.join(__dirname,'public/assets')));
app.use('/route',router);

app.use(bodyparser.urlencoded({extended:true}));
app.use(bodyparser.json());

app.use(session({
    secret:'secret',
    resave:'false',
    saveUninitialized:'true'
}));

app.get('/',(req,res) =>{
    res.render('base',{title:'Login'})
})
app.listen(port,console.log("listening.."));

router.js

const express = require('express');
const router = express.Router();

const user = {
    email : 'abc@gmail.com',
    password : 'abcd'
};

router.post('/login',(req,res)=>{

    console.log(req.body.myemail);
    res.end("Login Successful");

});

module.exports = router;

base.ejs

<%- include('header') -%>
    

<div class="div text-center center-div" id="login">
    <div class="container w-25 border py-5">
        <div class="title pb-5 ">
            <h2 class="font-weight-bold">Login System</h2>
            <span>Login for the existing user</span>
        </div>
        <form action="/route/login" method="POST"class="pt-0">
            <div class="form-group">
            <input type="email" class="form-control" placeholder="Email Id" name="myemail">
            <small class="form-text text-muted">Register Email address</small>
        </div>
            <div class="form-group">
                <input type="password" class="form-control" placeholder="Password" name="mypassword">
            </div>
            <button type="submit" class="btn btn-success rounded-pill">Submit</button>
        </form>
    </div>
</div>

<%- include('footer') -%>
jayasree
  • 3
  • 4
  • Move middleware before applying the routes it should handle. – raina77ow Jun 30 '21 at 17:23
  • I tried that still facing same issue..Can you suggest me – jayasree Jun 30 '21 at 17:38
  • Can you show the code with fix applied, and .use(bodyparser...) lines going before the .use('/route') ones? – raina77ow Jun 30 '21 at 17:40
  • const bodyparser = require('body-parser'); const router = require('./router'); const app = express(); const port = process.env.PORT || 5000; const bodyParser = require('body-parser'); app.use(bodyparser.urlencoded({extended:true})); app.use(bodyparser.json()); app.set('viewengine','ejs');app.use('/static',express.static(path.join(__dirname,'public')));app.use('/assets',express.static(path.join(__dirname,'public/assets'))); app.use(session({ secret:'secret',resave:'false',saveUninitialized:'true'})); app.use('/route',router); app.listen(port,console.log("listening..")); – jayasree Jun 30 '21 at 18:04
  • Please add this to the question. I've reopened the question if (it seems) that the reasons are actually different. – raina77ow Jun 30 '21 at 18:18

0 Answers0