[英]Issue Setting new routes in Express.js
我正在使用 express.js 創建一個應用程序,並且有兩個可以導航到的 ejs 頁面,但希望能夠導航到更多 ejs(我使用 pug)pug,因此我的應用程序可以是動態的,我想要更多我的鏈接和 hrefs 才能使用這些路由。
This page works and uses ejs <pug):
// LOGIN PAGE
app.post('/', ifLoggedin, [
body('user_email').custom((value) => {
return dbConnection.execute('SELECT `email` FROM `users` WHERE `email`=?', [value])
.then(([rows]) => {
if (rows.length == 1) {
return true;
}
return Promise.reject('Invalid Email Address!');
});
}),
body('user_pass', 'Password is empty!').trim().not().isEmpty(),
], (req, res) => {
const validation_result = validationResult(req);
const { user_pass, user_email } = req.body;
if (validation_result.isEmpty()) {
dbConnection.execute("SELECT * FROM `users` WHERE `email`=?", [user_email])
.then(([rows]) => {
bcrypt.compare(user_pass, rows[0].password).then(compare_result => {
if (compare_result === true) {
req.session.isLoggedIn = true;
req.session.userID = rows[0].id;
res.redirect('/');
}
else {
res.render('login-register', {
login_errors: ['Invalid Password!']
});
}
})
.catch(err => {
if (err) throw err;
});
}).catch(err => {
if (err) throw err;
});
}
else {
let allErrors = validation_result.errors.map((error) => {
return error.msg;
});
// REDERING login-register PAGE WITH LOGIN VALIDATION ERRORS
res.render('login-register', {
login_errors: allErrors
});
}
});
// END OF LOGIN PAGE
我試圖創建一條到這里的路線,但它不起作用,頁面返回 404:這是代碼中斷
// Testing >>>
app.get('/test', (req, res) => {
// render testicles
res.sendFile('test.ejs');
});
// Testing ^^^
您需要直接將絕對路徑與res.sendFile
一起使用。
res.sendFile(path.join(__dirname, './view', 'test.ejs'))
或者
res.sendFile('test.ejs', { root: path.join(__dirname, './views') });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.