gpt4 book ai didi

javascript - 使用 bodyParser 和 Router() 无法检索表单输入

转载 作者:行者123 更新时间:2023-11-28 00:46:46 25 4
gpt4 key购买 nike

我正在尝试创建一个将数据传递到注册路径的注册表单。但是,我无法在注册路径中检索表单输入。请建议如何检索 api.js 文件中的表单数据。我尝试使用 POSTMAN 将数据发送到/api/registration 端点,这确实在数据库中创建了一个用户。当我真正使用html表单注册时,它失败了。

HTML

<!DOCTYPE html>
<html>
<body>
<form action="/api/register" enctype="multipart/form-data" method="post">
<input name="email" type="text" placeholder="Email">
<input name="password" type="password" placeholder="Password">
<input name="passwordConf" type="password" placeholder="Confirm Password">
<input type="submit" value="Register">
</form>
</body>
</html>

Index.js

    //----setting up modules
const express = require('express');
const hbs = require('hbs');
const path = require('path');
var bodyParser = require('body-parser');
var mongoose = require('mongoose');
mongoose.Promise = global.Promise;
mongoose.connect(process.env.MONGODB_URI || 'mongodb://localhost/test');


//----define port connection on various environmentls
const port = process.env.PORT || 3000;

//----createing express app
var app = express();

// parse incoming requests
app.use(bodyParser.json());

//----use express middle-ware to utitlize public folder
// app.use(express.static(__dirname + 'public'));
app.use('/public', express.static(path.join(__dirname,'public')));
//see the first argument in above line it assigns which directory path is used to access the public file through URL


//----initializing handlebars as express view engine
app.set('view engine', 'hbs');

//----define handlebars partials and helpers
hbs.registerPartials(__dirname + '/views/partials');
//----define helper to get current year
hbs.registerHelper('getDate', ()=>{
return new Date().getFullYear();
});

//----define routes
app.get('/', function (req, res) {
res.render('home.hbs')
})

// include routes
app.use('/api', require('./routes/api'));
app.use('/', require('./routes/page'));

//----connecting to port
app.listen(port,()=>{
console.log(`success connection to port ${port}`);
})

Api.js 路由

var express = require('express');
var router = express.Router();
var {User} = require('./../models/user');
var {authenticate} = require('./../middleware/authenticate');
var _ = require('lodash');
var bodyParser = require('body-parser');
var jsonParser = bodyParser.json()
var urlencodedParser = bodyParser.urlencoded({ extended: false })


router.post('/register',function (req, res) {
console.log(req.body);
});
module.exports = router;

pages.js

    var express = require('express');
var router = express.Router();

router.get('/register', function (req, res) {
res.render('register.hbs', {title:"REGISTER"} )
})

router.get('/login', function (req, res) {
res.render('login.hbs', {title:"LOGIN"} )
})

module.exports = router;

最佳答案

这是我的建议:

创建一个名为 routes.js 的文件并在其中包含所有路由并导入它。

var express = require('express');
var router = express.Router();

app.get('/', function (req, res) {
res.render('home.hbs')
})

router.get('/api/register', function (req, res) {
res.render('register.hbs', {title:"REGISTER"} )
})

router.post('/api/register',function (req, res) {
//do your post logic here
console.log(req.body);
});

router.get('/login', function (req, res) {
res.render('login.hbs', {title:"LOGIN"} )
})

module.exports = router;

在你的 Index.js 中:

//Make sure the route is correct

app.use('/', require('/routes/routes.js'));

这样做,我希望错误得到解决,祝你好运!

关于javascript - 使用 bodyParser 和 Router() 无法检索表单输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49973182/

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