0

我在使用 expressjs 时出错。它说“无法获取 /register.html”。我不知道如何解决这个问题。我想向 /register 显示 register.html,但我总是收到错误消息。顺便说一句,这是我的代码伙计们。谢谢大家,我希望你能帮助我,我已经用谷歌搜索了这个错误 1 ​​小时,但我仍然无法让它工作。

应用程序.js

var express = require("express");
var app = express();
var redis = require("redis");
var client = redis.createClient();
var reply;

app.use(express.bodyParser());

client.on("error", function (err) {
console.log("error event - " + client.host + ":" + client.port + " - " + err);
});

app.get("/", function (req, res) {
res.sendfile(__dirname + "/login.html");
});

app.get("/", function (req, res) {
res.sendfile(__dirname + "/register.html");
});

app.post("/login", function (req, res) {
res.sendfile(__dirname + "/login.html");
console.log("--------------------");
console.log(req.body.user.username);
console.log(req.body.user.password);
console.log("--------EOF---------");
client.hmget( "credentials", req.body.user.username, function (err,pass) {
    console.log("req.body.username: " + req.body.user.username);
    console.log("req.body.password: " + req.body.user.password);
    console.log("pass: " + pass );
    console.log("err: " + err );

   if ( (!err) && pass && pass == req.body.user.password )
       res.write( "Successfully logged in!" );
   else if ( pass == false) 
    res.write("Account Does not Exist")
   else
       res.write( "Password is incorrect");
   res.end();
 });
});


app.post("/register", function (req, res) {
res.redirect("/register.html");
console.log("--------------------");
console.log(req.body.user.fname);
console.log(req.body.user.lname);
console.log(req.body.user.username);
console.log(req.body.user.pass);
console.log(req.body.user.email);
console.log(req.body.user.mobile);
console.log(req.body.user.mydate);

console.log("--------EOF---------");
client.hmset("credentials",req.body.user.uname, "first_name",req.body.user.fname, 
"last_name",req.body.user.lname, "username", req.body.user.uname, 
"password", req.body.user.pass, "email", req.body.user.email,
"mobile", req.body.user.mobile, redis.print);
res.write("data inserted");
res.end();

});

app.listen(1337);

这是我的login.html

<html>
<head></head>
<body>
<p>
<form method="post" action="/login">

Username: <input type="text" name="user[username]"> <br>
Password: <input type="text" name="user[password]"> <br>

<input type="submit" value="Submit"> <br>
<a href="/register.html">Register!</a>
</form>
</p>
</body>
</html>

这是我的register.html

<html>
<head></head>
<body>
<p>
<form method="post" action="/register">
First Name: <input type="text" name="user[fname]"> <br>
Last Name: <input type="text" name="user[lname]"> <br>
Username: <input type="text" name="user[username]" > <br>
Password: <input type="text" name="user[password]"> <br>
Email: <input type="text" name="user[email]"> <br>
Mobile Number: <input type="text" name="user[mobile]"> <br>
<input type="submit" value="Submit">
</p>
</form>
</body>
</html>
4

1 回答 1

1

您的 /register 路由应如下所示:

app.get("/register", function (req, res) {
  res.sendfile(__dirname + "/register.html");
});

目前您已将登录和注册视图都放在“/”上,但只有第一个可以工作。

于 2013-08-28T12:26:37.387 回答