我正在尝试使用node.js,html和js创建注册页面。我的登录命令有效,但我尝试复制和修改相同的代码,但无法注册。它在浏览器中显示“类型错误”。服务器工作正常。我尝试使用curl发送命令,它也可以正常工作。
curl -XPOST -H 'Content_Type: application/json' --data '{"username":"rkmenon235","password":"rkm235"}' http://eorlundgraymane.imad.hasura-app.io/create-user
我使用网页发布时收到的错误是
TypeError: Not a buffer
at TypeError (native)
at pbkdf2 (crypto.js:562:20)
at Object.exports.pbkdf2Sync (crypto.js:553:10)
at hash (/home/rkm/Desktop/webapp/imad-2016-app/server.js:22:22)
at /home/rkm/Desktop/webapp/imad-2016-app/server.js:62:17
at Layer.handle [as handle_request] (/home/rkm/Desktop/webapp/imad-2016-app/node_modules/express/lib/router/layer.js:95:5)
at next (/home/rkm/Desktop/webapp/imad-2016-app/node_modules/express/lib/router/route.js:131:13)
at Route.dispatch (/home/rkm/Desktop/webapp/imad-2016-app/node_modules/express/lib/router/route.js:112:3)
at Layer.handle [as handle_request] (/home/rkm/Desktop/webapp/imad-2016-app/node_modules/express/lib/router/layer.js:95:5)
at /home/rkm/Desktop/webapp/imad-2016-app/node_modules/express/lib/router/index.js:277:22
这是我用于注册命令的Javascript代码(main.js)。
var regy = document.getElementById('signupbutton');
regy.onclick = function(){
var request = new XMLHttpRequest();
request.onreadystatechange = function(){
if(request.readyState === XMLHttpRequest.DONE){
if(request.status === 200)
{
console.log("user created");
alert("Creates Account successfully");
}
else if(request.status === 403){
alert('Something is 404');
}
else if(request.status === 500){
alert('Something is wrong on the server 500');
}
}
};
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
console.log(username);
console.log(password);
request.open('POST','http://localhost:8080/create-user',true);
request.setRequestHeader('Content-Type', 'application/json');
request.send(JSON.stringify({username:username,password:password}));
};
这是我的服务器代码(server.js),它响应POST请求
我的服务器代码顶部有
var express = require('express');
var morgan = require('morgan');
var path = require('path');
var Pool = require('pg').Pool;//Postgres connetion pool
var crypto = require('crypto');
var bodyParser = require('body-parser');
var app = express();
app.use(morgan('combined'));
app.use(bodyParser.json());
然后我有这个特定的请求处理程序(也是server.js的一部分)
app.post('/create-user',function(req,res){
var username = req.body.username;
var password = req.body.password;
var salt = crypto.randomBytes(128).toString('hex');
var dbString = hash(password,salt);
pool.query('INSERT INTO users (username,passwordhash) VALUES ($1,$2)',[username,dbString],function(err,result){
if(err){
res.status(500).send(err.toString());
}
else
{
res.send('User Successfully created !'+username);
}
});
});
这是HTML代码
<html>
<link href = "/loginstyle.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type = "text/javascript" src = "/scripts/coderider"></script>
<body id = "bodyback">
<div id = "container">
<div id = "loginpane">
<form action = "/create-user" method = "POST">
<input type = "text" name = "username" id = "username" placeholder = "Username"/><br>
<input type = "password" name = "password" id = "password" placeholder = "Password"/>
<br>
<input type = "submit" id = "signupbutton" name = "signupbutton" value = "Sign Up"/>
<br>
</form>
</div>
</div>
</body>
</html>
<script type = "text/javascript" src = "/main.js"></script>
我在终端使用CURL时重复代码工作正常但我无法使用javascript从html页面使用POST调用注册。
感谢您提供的任何帮助。
答案 0 :(得分:0)
我认为您遇到跨域请求问题。 尝试将标头添加到Node.js
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");
答案 1 :(得分:0)
问题在于我已将登录和提交页面的脚本链接指定给同一文件。文本元素是在同一个脚本中创建的,但是有两个不同的文件。这不是一个保证的任务,因为元素不能使用document.getElementbyId();
进行调用。所以我为每个页面创建了一个单独的脚本并调用了document元素。这次我测试使用console.lo进行调试,它就像一个魅力