我已经为我的网站进行了身份验证,并且试图用一些信息填充注册页面。 当我填写该信息时,我在安装或注册时会有一个click事件,该事件会重定向到Bad Request页面。你能帮忙吗?
app.js代码
//jshint esversion:6
const express = require("express");
const ejs = require("ejs");
const bodyParser = require("body-parser");
const mongoose = require("mongoose");
const session = require("express-session");
const passport = require("passport");
const passportLocalMongoose = require("passport-local-mongoose");
const app = express();
app.use(express.static("public"));
app.set('view engine', "ejs");
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(session({
secret: 'Thisismynameisdo',
resave: false,
saveUninitialized: true,
}));
app.use(passport.initialize());
// create session
app.use(passport.session());
mongoose.connect("mongodb://localhost:27017/myPortfolio", {useNewUrlParser: true});
const adminUser = new mongoose.Schema ({
flname : String,
username : String,
email : String,
password: String
});
// hash and salt password
adminUser.plugin(passportLocalMongoose);
const Admin = mongoose.model("Admin", adminUser);
// create strategies
passport.use(Admin.createStrategy());
passport.serializeUser(Admin.serializeUser());
passport.deserializeUser(Admin.deserializeUser());
app.get("/", function(req, res) {
res.render("frontend/index");
});
app.get("/blog", function(req, res) {
res.render("frontend/blog");
});
app.get("/404", function(req, res) {
res.render("frontend/404");
});
app.get("/blogdetail", function(req, res) {
res.render("frontend/blogdetail");
});
app.get("/blogaudio", function(req, res) {
res.render("frontend/blogaudio");
});
app.get("/login", function(req, res) {
res.render("backend/login");
});
app.get("/thanks", function(req, res) {
res.render("frontend/mailchimp");
});
app.get('/install', function(req, res) {
res.render("backend/install.ejs");
});
app.get("/admin", function(req, res) {
if(req.isAuthenticated()) {
res.render("backend/dashboard");
} else {
res.redirect("/login");
}
});
app.post("/install", function(req, res) {
//registration page
Admin.register({flname: req.body.flname, username: req.body.username, email: req.body.email}, req.body.pass, function(err, user) {
if (err) {
console.log(err);
res.redirect("/install");
} else {
passport.authenticate("local")(req, res, function() {
res.redirect("/admin");
});
}
});
});
app.post("/login", function(req, res) {
const user = new Admin({
flname : req.body.flname,
username : req.body.username,
email : req.body.email,
password: req.body.pass
});
req.login(user, function(err){
passport.authenticate("local")(req, res, function() {
res.redirect("/admin");
});
});
});
app.listen(3000, function(req, res) {
console.log("running => 3000");
});
我使用页面的ejs查看内容和正文解析器来获取 应用js文件中的代码。
我的注册页面代码如下:
管理区
<form action="/install" method="POST" class="login100-form validate-form"> <span class="login100-form-title"> Installation Process </span> <div class="wrap-input100 validate-input" data-validate = "enter Full name like : Ismail Taibi"> <input class="input100" type="text" name="flname" placeholder="Full Name"> <span class="focus-input100"></span> <span class="symbol-input100"> <i class="fa fa-users" aria-hidden="true"></i> </span> </div> <div class="wrap-input100 validate-input" data-validate = "Valid username is required: abcd123"> <input class="input100" type="text" name="username" placeholder="Username"> <span class="focus-input100"></span> <span class="symbol-input100"> <i class="fa fa-user" aria-hidden="true"></i> </span> </div> <div class="wrap-input100 validate-input" data-validate = "Valid email is required: ex@abc.xyz"> <input class="input100" type="email" name="email" placeholder="Email"> <span class="focus-input100"></span> <span class="symbol-input100"> <i class="fa fa-envelope" aria-hidden="true"></i> </span> </div> <div class="wrap-input100 validate-input" data-validate = "Password is required"> <input class="input100" type="password" name="pass" placeholder="Password"> <span class="focus-input100"></span> <span class="symbol-input100"> <i class="fa fa-lock" aria-hidden="true"></i> </span> </div> <div class="container-login100-form-btn"> <button type="submit" name="btnRegistrationSubmit" class="login100-form-btn"> INSTALL </button> </div> <div class="text-center p-t-136"> <a class="txt2" href="https://www.facebook.com/profile.php?id=100011693180444"> Developed by Ismail Taibi <i class="fa fa-long-arrow-right m-l-5" aria-hidden="true"></i> </a> </div> </form> </div> </div> </div>
$('.js-tilt').tilt({ scale: 1.1 }) </script> <!--===============================================================================================--> <script src="admin/js/main.js"></script>