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 ?
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>
Step by step
Solved in 3 steps