我正在关注模块p-11(身份验证,散列,卷曲等)并想要创建用户,但它不知何故无法正常工作。 问题是,只要我点击提交按钮 我得到的不是缓冲区错误。 不确定是什么问题。
我一直收到错误:TypeError:不是缓冲区
我已经需要body-parser和crypto。 这是server.js中的相关代码:
function hash(input,salt){
//how to create hash
var hashed = crypto.pbkdf2Sync(input,salt, 10000, 512, 'sha512');
return ['pbkdf2Sync',salt,hashed.toString('hex')].join('$');
}
app.post('/create-user',function(req,res){
//take username and password as input and create entry in user table
var username = req.body.username;
var password = req.body.password;
console.log(username);
var salt= crypto.randomBytes(128).toString('hex');
var dbString = hash(password,salt);
pool.query('INSERT INTO "user"(username,password) VALUES($1,$2)',[username,dbString],function(err,result){
if(err){
res.status(500).send(err.toString());
}else{
res.send('user succesfully created'+username);
}
});
});
app.get('/hash/:input',function(req,res){
var hashString = hash(req.params.input,'this-is-some-random-string');
res.send(hashString);
});
这是main.js中的相关代码
var submitButton = document.getElementById('submit_btn');
submitButton.onclick = function(){
var request = new XMLHttpRequest();
request.onreadystatechange=function(){
if(request.readyState===XMLHttpRequest.DONE && request.status ===200){
var data = JSON.parse(request.responseText);
console.log(data);
}
};
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
request.open('POST','http://localhost:8080/create-user',true);
request.send(JSON.stringify({username:username,password:password}));
};
这是我收到的错误消息
TypeError: Not a buffer
at TypeError (native)
at pbkdf2 (crypto.js:562:20)
at Object.exports.pbkdf2Sync (crypto.js:553:10)
at hash (D:\imadtw\server.js:119:23)
at D:\imadtw\server.js:129:18
at Layer.handle [as handle_request] (D:\imadtw\node_modules\express\lib\router\layer.js:95:5)
at next (D:\imadtw\node_modules\express\lib\router\route.js:131:13)
at Route.dispatch (D:\imadtw\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (D:\imadtw\node_modules\express\lib\router\layer.js:95:5)
at D:\imadtw\node_modules\express\lib\router\index.js:277:22
at Function.process_params (D:\imadtw\node_modules\express\lib\router\index.js:330:12)
at next (D:\imadtw\node_modules\express\lib\router\index.js:271:10)
at jsonParser (D:\imadtw\node_modules\body-parser\lib\types\json.js:112:7)
at Layer.handle [as handle_request] (D:\imadtw\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (D:\imadtw\node_modules\express\lib\router\index.js:312:13)
at D:\imadtw\node_modules\express\lib\router\index.js:280:7
编辑:我刚试了一下。
这个过程工作正常,当我硬编码用户名和密码的值(就像一个实验),但当我尝试使用req.body获取用户名和密码的值时它不起作用
似乎从身体获取价值是这里的难点。
这是我希望从
输入值的页面的html<!doctype html>
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- jQuery library -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<!-- Latest compiled JavaScript -->
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<!-- Fontawesome library -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css">
<link rel="stylesheet" type="text/css" href="/ui/profile.css">
<script type="text/javascript" src="/ui/main.js" ></script>
</head>
<body>
<!--Navbar Begins -->
<nav class="navbar navbar-fixed-top scrolltop" id="navigation">
<div class="container">
<!-- navbar header begins -->
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar-collapse">
<span class="sr-only">Toggle Navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="#">Faraz Ahmed</a>
</div>
<!-- navbar header ends -->
<!-- div collapse begins -->
<div class="collapse navbar-collapse" id="navbar-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="active"><a href="#aboutme">About</a></li>
<li><a href="#portfolioz">Portfolio</a></li>
<li><a href="#contactme">Contact</a></li>
</ul>
</div>
<!-- div collapse ends -->
</div>
<!-- container ends -->
</nav>
<!--Navbar ends -->
<div class="container-fluid">
<div class="row" id="fullBody">
<div class=" col-md-12 col-sm-12 col-xs-12" id="column-main">
<div id="aboutme" class="about">
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<div id="img"><img class="img-responsive" src="http://res.cloudinary.com/whizzy/image/upload/v1454699647/faraz_qwdqrx.jpg" alt=""></div>
<!-- <hr class="hr1"><i class="fa fa-star"></i><hr class="hr2"> -->
<p class="aboutpara scrollz"></br>I am a self taught web developer who has learnt html,css, Javascript,Jquery and bootstrap too. I keep building websites and other apps to practice what I have learnt. This site is responsive. Go ahead and take a look at it in your mobile and see how they adjust to your screen size.</p>
</div>
</div>
</div><!-- aboutme div ends -->
<div class="user">
<p>
<form method="post" action="/create-user" >
<input type="text" id="username" placeholder="username">
<input type="password" id="password" name="password">
<input type="submit" id="submit_btn" value="Submit" class="btn btn-warning">
</form>
</p>
<ul id="namelist">
</ul>
</div><!-- requestz div ends -->
<div id='twitterDiv' class="row">
<div class="col-lg-12 col-md-12 col-sm-12">
<h3>Tweets Here</h3>
<input type="text" id="tweetSearch" placeholder="enter keyword to search for"></input>
<button id="tweetButton" type="submit">Submit</button>
<p id="tweetz">tweets appear here</p>
</div><!--column ends -->
</div><!--row ends -->
</div><!--2nd column ends -->
</div><!-- main row ends -->
</div><!-- container-fluid div ends -->
</body>
</html>
答案 0 :(得分:0)
确保您正在设置正确的正文内容类型:
request.open('POST','http://localhost:8080/create-user',true);
request.setRequestHeader('content-type', 'application/json');
request.send(JSON.stringify({username:username,password:password}));
答案 1 :(得分:0)
根据您的html,您没有为用户名字段设置name
属性。这意味着在提交html表单时(与XHR分开),该字段不会被发送到服务器。
编辑:这可能与pbkdf2Sync()
的特定问题无关,因为它不考虑用户名值。