i am writing a nodejs login page ,that can read username and passeord from a json file . but my job cant work ,can someone help me see whats the problem in my code ?

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
100%

i am writing a nodejs login page ,that can read username and passeord from a json file .

but my job cant work ,can someone help me see whats the problem in my code ?

app.js

const express = require('express');

const app = express();

const bodyParser = require('body-parser');

const fs = require('fs');

var createError = require('http-errors');

var express = require('express');

var path = require('path');

const session = require("express-session");

var cookieParser = require('cookie-parser');

var logger = require('morgan');

var app = express();

 

app.set('views', path.join(__dirname, 'views'));

app.set('view engine', 'hbs');

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(passport.initialize());

app.use(passport.session());

app.use(express.static('./public'));

app.use(bodyParser.urlencoded({extended:false}))

app.use(bodyParser.json());

 

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

fs.readFile('./data/data.json',(err,data)=> {

var arr=[];

if(err){

console.log(err);

}else{

if(data.toString()){

arr=JSON.parse(data.toString())

}

var s = arr.find((item)=>{

if(item.name == req.body.name){

return item

}

})

if(s){

if(s.password == req.body.password){

res.json({

status:'y',

meg:'login success',

data:s.name

})

}else{

res.json({

status:'err',

meg:'wrong password ',

})

}

}else{

res.json({

status:'n',

meg:'no such user ',

})

}

}

})

});



app.listen(3000,function () {

console.log(`Listening on port ${PORT}`)

});

----

login .hbs

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>login</title>

</head>

<body class="container">

<div class="row">

<div class="col-12">

<ul class="list-group">

<li class="list-group-item"><input class="form-control" type="text" id="name" placeholder="nter account"></li>

<li class="list-group-item"><input class="form-control" type="text" id="pwd" placeholder="enter password"></li>

<li class="list-group-item"><button type="button" class="btn btn-block btn-danger" id="btnLogin"login</button></li>

</ul>

</div>

</div>

</body>

</html>

-----------

mainpage .hbs

<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="UTF-8">

<title>mainpage </title>

<meta name="viewport" content="width=device-width, initial-scale=1">

<link rel="stylesheet"

</head>

<body class="container">

<span>username:</span>

<span id="username"></span>

</html>

Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education