Javascript EJS未在显示时呈现页面
请某人 下面是我尝试的一个登录应用程序的代码,我相信你能猜到我在这方面真的是个笨蛋,我想我做的一切都是对的,因为没有抛出任何错误,但我遇到了这个问题,它一直在折磨我。 我一直在绞尽脑汁寻找答案,但我就是无法让ejs渲染视图。检查网络后,显示获取项是正确的,但它只是不呈现 obj.jsJavascript EJS未在显示时呈现页面,javascript,node.js,express,ejs,Javascript,Node.js,Express,Ejs,请某人 下面是我尝试的一个登录应用程序的代码,我相信你能猜到我在这方面真的是个笨蛋,我想我做的一切都是对的,因为没有抛出任何错误,但我遇到了这个问题,它一直在折磨我。 我一直在绞尽脑汁寻找答案,但我就是无法让ejs渲染视图。检查网络后,显示获取项是正确的,但它只是不呈现 obj.js const form = document.getElementById('Signinform'); form.addEventListener('submit', Signin); async functio
const form = document.getElementById('Signinform');
form.addEventListener('submit', Signin);
async function Signin(event){
event.preventDefault();
const email = document.getElementById('email').value
const password = document.getElementById('password').value
const result = await fetch('/user/signin',{
method:'POST',
headers:{
'Content-Type':'application/json',
'Accept': 'text/html'
},
body:JSON.stringify({
email,
password
})
})//.then((res)=>res.json())
//console.log(result)
})
User.js
const express = require('express');
const router =express.Router();
//mongodb user model
const user = require('./../models/user');
const bodyParser = require('body-parser');
//password handler
const bcrypt = require('bcrypt');
//making the router use request body as json
//Signup
router.post('/signup',(req,res)=>{
let{name, email, password, dateofBirth} = req.body;
name = name.trim();
email = email.trim();
password = password.trim();
dateofBirth = dateofBirth.trim();
if(name == "" || email == "" || password == "" || dateofBirth == ""){
res.json({
status:"FAILED",
message:"Empty input fields"
});
}else if(!/^[a-zA-Z]*$/.test(name)) {
res.json({
status:"FAILED",
message:"Invalid name entered"
});
}else if(!/^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$/.test(email)){
res.json({
status:"FAILED",
message:"Invalid name entered"
});
}else if(! new Date(dateofBirth).getTime()){
res.json({
status:"FAILED",
message:"Invalid date of Birth entered"
});
}else if(password.length < 8){
res.json({
status:"FAILED",
message:"Password is too short!!"
})
}else{
//Checking if user already exists
user.find({email}).then(result=>{
if (result.length){
//if user already exists
res.json({
status: "FAILED",
message: "User with the provided email still exists"
})
} else{
//Try to create a new user
//password handling
const saltRounds =10;
bcrypt.hash(password,saltRounds).then(hashedPassword =>{
const newUser = new user({
name,
email,
password:hashedPassword,
dateofBirth
});
newUser.save().then(result=>{
res.json({
status: "SUCCESS",
message:"Signup succesful",
data:result,
})
}).catch(err=>{
res.json({
status:"FAILED",
message:"An error occured while trying to save User"
})
})
}).catch(err=>{
res.json({
status:"FAILED",
message:"An error occured while hashing password!"
})
})
}
}).catch(err=>{
console.log(err);
res.json({
status:"FAILED",
message:"An error occurered while checking for existing user"
})
})
}
})
//Rendering Sign in page
router.get('/signin',async(req,res)=>{
res.render('user_signin');
});
//Signin
router.post('/signin', async(req,res)=>{
let{email, password}=req.body;
email = email.trim();
password = password.trim();
if(email == "" || password == ""){
res.json({
status:"FAILED",
message:"Empty Credentials supplied"
});
}else{
//Check if user exists
user.find({email})
.then(data =>{
if(data.length){
//user exists
const hashedPassword = data[0].password;
bcrypt.compare(password,hashedPassword).then(result=>{
if(result){
//password matches
// res.json({
// status:"SUCCESS",
// message:"Signin succesful",
// data : data
// });
res.render('signinsuccess',{name : data[0].name});
}else{
res.json({
status: "FAILED",
message: "Invalid password entered"
});
}
})
.catch(err=>{
res.json({
status: "FAILED",
message:"An error occured while comparing passwords"
})
})
}else{
res.json({
status:"FAILED",
message:"Invalid Credentials entered"
})
}
})
.catch(err=>{
res.json({
status:"FAILED",
message:"An error occured while checking for existing user"
})
})
}
})
module.exports = router;
请帮助什么是
登录成功
res.render
要求完整声明ejs文件,即res.render('somthing.ejs')
我假设在声明视图引擎后,渲染函数会自动将输入的名称作为ejs。但根据你的建议,我试着改变它,但仍然不起作用:(您进行了哪些调试?您是否使用console.log
logging检查了所有预期功能是否正常运行?Mmmmm以便为您提供上下文用户。js处理登录和注册部分的主体,这些部分最初是通过控制台日志进行调试和检查的,并使用postman查看实际的fun可选性。但我现在的目标是为已经创建的后端创建一个前端,我遇到的第一个问题是ejs没有呈现。您正在尝试的url是什么?什么是signInsAccess
?res.render
需要完整声明ejs文件,即res.render('somthing.ejs'))
我假设在声明视图引擎后,渲染函数会自动将输入的名称作为ejs。但根据您的建议,我尝试更改它,但仍然不起作用:(您进行了哪些调试?您是否使用console.log
logging检查了所有预期功能是否正常运行?Mmmmm以便为您提供上下文用户。js处理登录和注册部分的主体,这些部分最初是通过控制台日志进行调试和检查的,并使用postman查看实际的fu但是我现在的目标是为已经创建的后端创建一个前端,我遇到的第一个问题是ejs没有呈现。您正在尝试的url是什么?
var express = require('express');
//getting database
require('./config/db.js');
const app = require('express')();
const port = 3000;
app.set('view engine','ejs');
const UserRouter =require('./api/user');
app.use('/assets', express.static('assets'));
//For accepting post form data
const bodyparser = require('express').json;
app.use(bodyparser());
app.use('/user', UserRouter)
app.listen(port,()=>{
console.log(`Server running on port ${port}`);
})