gpt4 book ai didi

html - Express 正确定义 post 路由方法

转载 作者:太空宇宙 更新时间:2023-11-04 01:38:35 32 4
gpt4 key购买 nike

我在正在处理的应用程序上使用带有 Node 的express.js 时遇到路由错误。我无法正确处理 post 请求,并收到 http 500 错误,这是我的 server.js 文件代码

var express = require('express');
var app = express();
app.set('view engine', 'ejs');
var expressValidator = require('express-validator');
app.use(expressValidator());
var router = express.Router();
app.use(router);


var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());

var methodOverride = require('method-override');
app.use(methodOverride(function (req, res) {
if (req.body && typeof req.body === 'object' && '_method' in req.body) {
var method = req.body._method;
delete req.body._method;
return method
}
}));

var flash = require('express-flash');
var cookieParser = require('cookie-parser');
var session = require('express-session');
app.use(cookieParser('csci3308'));
app.use(session({
secret: 'csci3308',
resave: false,
saveUninitialized: true,
cookie: {maxAge: 60000}
}));
app.use(flash());



var login = require('./routes/login');
var success = require('./routes/success');
var profileCreation = require('./routes/profileCreation');
var calendar = require('./routes/calendar');
var student = require('./routes/student');
var professor = require('./routes/professor');

app.use(express.static('public'));
app.use('/', login);
app.use('/login', login);
app.use('/success', success);
app.use('/profileCreation', profileCreation);
app.use('/calendar', calendar);
app.use('/student', student);
app.use('/professor', professor);




//error handling
app.use((req, res, next) => {
console.log(req.url)
console.log(req.method)
console.log(req.params)
console.log(req.body)
res.status(404).send("Sorry can't find that!")
})

app.use((err, req, res, next) =>{
console.error(err.stack)
res.status(500).send('Something broke!')
})


var port = 4000;
app.listen(port, function () {
console.log('Server running on http://localhost:' + port)
});

这是我的 profileCreation.js 代码

var express = require('express');
var router = express.Router();
var expressValidator = require('express-validator');
router.use(expressValidator());
const { check } = require('express-validator/check');
var db = require('../database.js');



router.post('/profileCreation', function(request, response){
console.log("made it to profileCreation post route")
response.redirect('/login')
//request.assert('email', 'email is required').notEmpty();
// request.assert('pswd', 'password is required').notEmpty();
// request.assert('cfmPswd', 'confirm password is required').notEmpty();
// request.assert('pswd', 'password and confirm password are not the same').equals('cfmPswd');
// var queryCheckEmailNotTaken = 'select (email) from users where(email = $1)';
// if(db.oneOrNone(queryCheckEmailNotTaken, 'email')=== null){
// //make eroor message
// request.flash('error', 'Creation failed');
// response.render('profileCreation');
// }
// else{
// var isProf = document.getElementById("isProfessor").checked;
// //var dbQueryAddUserString = 'Insert into users(email, pswd, isProffessor) values(request.sanitize('email'), request.sanitize('pswd'), isProf;
// var dbQueryAddUserString = 'Insert into users(email, pswdID, isProffessor) values($1, $2, $3)';
// db.none(dbQueryAddUserString, [email,pswd, isProf]).then(
// response.render('login')
// ).catch(function(error){
// request.flash('error', 'Creation failed');
// response.render('profileCreation');
// })
// }
});




router.get('/', function (request, response) {
console.log('Made it to profile creation root route');
response.render('profileCreation', {data: {}});
});

router.get('/profileCreation', function(request, response){
console.log('Made it to profileCreation route');
response.render('profileCreation');
});


module.exports = router;

这是我的 profileCreation.ejs 代码

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="/profileCreation.css" type=text/css>

<title>Profile Creation Page</title>
</head>
<body>
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
<div class="wrapper" id="rootContainer">

<div class="container" id="outerBoxContainer">

<div class="container" id="contentContainer">

<div class="container" id="titleContainer">

<div class="row justify-content-center" id="titleRow">
<h1 id="loginHeader">DayBook Profile Creation</h1>
</div>

</div>


<div class="container" id="profileContainer">
<form method="post" action="/profileCreation">
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Email">
</div>

<div class="form-group">
<label for="passwrd">Password</label>
<input type="password" class="form-control" name="pswd" id="passwrd" placeholder="Password">
</div>

<div class="form-group">
<label for="cfmPasswrd">Confirm Password</label>
<input type="password" class="form-control" name="cfmPswd" id="cfmPasswrd" placeholder="Confirm Password">
</div>


<!-- <div class="radio">
<label>Role</label>
<br>
<label style="font-weight:normal"><input type="radio">Student</label>
<br>
<label style="font-weight:normal"><input type="radio">Professor</label>
<br>
</div> -->



<button type="submit" name="createProfileButton" >Create Profile</button>


</form>

</div>

</div>

</div>

</div>




</body>
</html>

当我尝试在配置文件创建路径上使用提交按钮时,收到 500 错误。我不知道是什么问题。这就像路由器没有正确定义处理 post 请求的路由。我使用与登录功能相同的公式,但由于某种原因它不起作用。本地服务器表示无法发布到路由。请帮我。我很感激任何反馈。我是这方面的业余爱好者,认为这可能只是某个地方的一个小错误。先感谢您!

最佳答案

您可以尝试使用访问

BASE_URL + 'profileCreation/profileCreation'

注意:将 BASE_URL 替换为您的实际基本 URL

关于html - Express 正确定义 post 路由方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53718227/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com