为什么我的POST请求会无限加载?

时间:2017-06-01 02:29:14

标签: javascript node.js mongodb passport-local passport.js

我正在尝试构建一个node.js应用程序,当我尝试对寄存器或登录方法进行发布请求时,它似乎继续加载而不做任何事情。我在网络浏览器中收到以下错误 - “localhost没有发送任何数据”。这是我设置代码的方式:

var express = require("express"),
app = express(),
bodyParser = require("body-parser"),
mongoose = require("mongoose"),
passport = require("passport"),
flash = require("connect-flash"),
LocalStrategy = require("passport-local"),
methodOverride = require("method-override"),
User = require("./models/user");

mongoose.connect("mongod://localhost/supportApp_v1");
app.use(bodyParser.urlencoded({extended:true}));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
app.use(flash());

app.use(require("express-session")({
secret: "luck",
resave: false,
saveUninitialized: false
}));

app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());

app.use(function(req,res,next){
  res.locals.currentUser = req.user;
  res.locals.error = req.flash("error");
  res.locals.success = req.flash("success");
  next();
});

app.get("/", function(req, res){
res.render("landing");
});

app.get("/register", function(req, res){
res.render("register");
});

app.post("/register", function(req, res){
var newUser = new User({username: req.body.username});
User.register(newUser, req.body.password, function(err, user){
    if (err) {
        return res.render("register", {"error": err.message});
    } else {
        passport.authenticate("local")(req, res, function(){
            req.flash("success", "Welcome " + user.username);
            res.redirect("/");
        });
    }
   });
 });

app.get("/login", function(req, res){
res.render("login");
});

app.post("/login", passport.authenticate("local",
{
    successRedirect: "/",
    failureRedirect: "/login",
}), function (req, res){
});

app.listen(process.env.PORT || 3000, process.env.IP, function(){
console.log("Server has started");
});

我使用的模型:

 var mongoose = require("mongoose");
 var passportLocalMongoose = require("passport-local-mongoose");

 var UserSchema = new mongoose.Schema({
 username: String,
 password: String,
 email: String,
 age: Number
});

UserSchema.plugin(passportLocalMongoose);

module.exports = mongoose.model("User", UserSchema);

和注册表格:

  <div class="container" id="regContainer">
 <h1>Register</h1>
 <hr>
 <form action="/register" method="POST">

    <div class="form-group row">
        <label for="regUser" class="col-sm-2 col-form-label">Username:
 </label>
        <div class="col-sm-10">
            <input id="regUser" type="text" name="username" placeholder="Username">
        </div>
    </div>

    <div class="form-group row">
        <label for="regUser" class="col-sm-2 col-form-label">Email:</label>
        <div class="col-sm-10">
            <input id="regUser" type="text" name="email" placeholder="Email">
        </div>
    </div>

    <div class="form-group row">
        <label for="regPass" class="col-sm-2 col-form-label">Password:</label>
        <div class="col-sm-10">
            <input id="regUser" type="password" name="password" placeholder="Password">
        </div>
    </div>

    <div class="form-group row">
        <label for="regPass" class="col-sm-2 col-form-label">Age:</label>
        <div class="col-sm-10">
            <input id="regUser" type="number" name="age" placeholder="Age">
        </div>
    </div>

<div class="form-group row">
    <div class="offset-sm-2 col-sm-10">
        <button type="submit" class="btn btn-primary">Register</button>
    </div>
</div>

1 个答案:

答案 0 :(得分:1)

Connection via Mongoose

mongoose.connect('mongodb://localhost/myapp');

你的遗失d。 @MariaInesParnisari也怀疑连接问题。我还注意到你的register.html在结尾附近遗漏了一些露水标签,例如</form>(可能是由于不完整的复制粘贴)。我也想知道你放置.html文件的位置,因为你没有设置'views'的路径,例如

app.set('views', path.join(__dirname, '/views'));

也许您使用的是默认设置?