gpt4 book ai didi

javascript - Express 4.x bodyparser 不工作

转载 作者:行者123 更新时间:2023-12-01 03:41:53 24 4
gpt4 key购买 nike

我安装了最新版本的 body-parser(1.17.1) 并按照其他人的建议按照正确的顺序设置了我的代码。

  1. 进口 express

  2. 导入正文解析器

  3. app = express();

  4. app.use(bodyParser.json());

  5. app.use(bodyParser.urlencoded({extended : false}));

但我不断收到错误“TypeError:无法读取未定义的属性“密码””。

其实我无法想象问题是什么。

我花了近 10 个小时不断跟踪解决方案,我几乎要放弃了。

有什么方法可以让我的代码工作吗?

请帮我一些......

下面是我的代码

    const express = require('express');
const path = require('path');
const session = require('express-session');
const MySQLStore = require('express-mysql-session')(session);
const bodyParser = require('body-parser');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const pbkdf2Password = require('pbkdf2-password');
const hasher = pbkdf2Password();
const mysql = require('mysql');
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
password: '10rhrnak',
database: 'users',
});
connection.connect();

//crawler setting
const http = require('http');
const cheerio = require('cheerio');
const iconv = require('iconv-lite');
const fs = require('fs');

const app = express();
const port = process.env.PORT || '4200';
app.set('port', port);
app.listen(port, () => console.log(`API running on localhost:${port}`));

app.use(express.static(path.join(__dirname, '../dist')));

app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../dist/index.html'));
});

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(session({
secret: '@232t2g23',
resave: false,
saveUninitialized: true,
store: new MySQLStore({
host: 'localhost',
port: 3306,
user: 'root',
password: '10rhrnak',
database: 'users',
})
}));
app.use(passport.initialize());
app.use(passport.session());

app.post('/auth/register', (res, req) => {

hasher({password: req.body.password}, (err, pass, salt, hash) => {
let user = {
authId: 'local'+req.body.email,
email: req.body.email,
password: hash,
salt: salt,
displayName: req.body.displayName
};
let sql = 'INSERT INTO users SET ?';
connection.query(sql, user, (err, results) => {
if(err) {
console.log(err);
throw new Error("register error!");
} else {
req.redirect('/');
}
});
});
});

还有我的前端代码(angular2 html模板)

<div class="loginbox">
<div [hidden]="submitted">
<header>MAILBOY</header>
<form ngNoForm action="/auth/register" method="post">
<input type="text" name="email" id="email" placeholder="이메일 주소"/>
<input type="password" name="password" id="password" placeholder="비밀번호"/>
<input type="text" name="displayName" id="displayName" placeholder="사용자 이름"/>
<button type="submit" class="login">가입</button>
</form>
</div>
</div>

最佳答案

您切换了路由处理程序的参数:

app.post('/auth/register', (res, req) => { ... });

应该是:

app.post('/auth/register', (req, res) => { ... });

关于javascript - Express 4.x bodyparser 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43817843/

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