使用简单的节点应用程序,本地主机不发送数据

时间:2018-06-25 18:42:52

标签: javascript node.js

我是新手,所以请客气。我一直在尝试通过在线教程(本one,github here)来学习护照,据我所知,我已经逐字重新创建了代码,但是却遇到了一个错误本地主机未发送任何数据。我的完整代码是here。我在下面粘贴了一些代码,但是老实说我不确定要发布哪段代码,因为我不知道错误发生在哪里。

这是服务器应用程序:

var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var exphbs = require('express-handlebars');
var expressValidator = require('express-validator');
var flash = require('connect-flash');
var session = require('express-session');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var mongo = require('mongodb');
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/loginapp');
var db = mongoose.connection;

var routes = require('./routes/index');
var users = require('./routes/users');

//Init app
var app = express();

//View engine
app.set('views', path.join(__dirname, 'views'));
app.engine('handlebars', exphbs({defaultLayout: 'layout'}));
app.set('view engine', 'handlebars');

//body parser middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());

//set static folder
app.use(express.static(path.join(__dirname, 'public')));

//express session middleware
app.use(session({
	secret: 'secret',
	saveUninitialized: true,
	resave: true
}));

//passport init
app.use(passport.initialize());
app.use(passport.session());

//validator middleware-- this code is straight from the validator github page
app.use(expressValidator({
	errorFormatter: function(param, msg, value) {
			var namespace = param.split(',')
			, root = namespace.shift()
			, formParam = root;

		while(namespace.length) {
			formParam += '[' + namespace.shift() + ']';
		}
		return {
			param 	: formParam,
			msg		: msg,
			value	: value 
		};
	}
}));

//connect flash middleware
app.use(flash());

//global variables for flash messages
app.use(function (req, res, next) {
		res.locals.success_msg = req.flash('success_msg');
		res.locals.error_msg = req.flash('error_msg');
		res.locals.error = req.flash('error');
		res.locals.user = req.user || null;
		next();
});


app.use('/', routes);
app.use('/users', users);

//set port
app.set('port', (process.env.PORT || 3000));

app.listen(app.get('port'), function() {
	console.log('Server started on port ' + app.get('port'));
});

这是表格:

<h2 class="page-header">Register</h2>
<form method="post" action="/users/register">
   <div class="form-group">
    <label>Name</label>
    <input type="text" class="form-control" placeholder="Name" name="name">
  </div>
  <div class="form-group">
    <label>Username</label>
    <input type="text" class="form-control" placeholder="Username" name="username">
  </div>
   <div class="form-group">
    <label>Email</label>
    <input type="email" class="form-control" placeholder="Email" name="email">
  </div>
  <div class="form-group">
    <label>Password</label>
    <input type="password" class="form-control" placeholder="Password" name="password">
  </div>
  <div class="form-group">
    <label>Confirm Password</label>
    <input type="password" class="form-control" placeholder="Password" name="password2">
  </div>
  <button type="submit" class="btn btn-default">Submit</button>
</form>

这是用户js:

var express = require('express');
var router = express.Router();

//register
router.get('/register', function(req, res) {
	res.render('register');
});

//login
router.get('/login', function(req, res) {
	res.render('login');
});

//register user
router.post('/register', function(req, res) {
	var name = req.body.name;
	var email = req.body.email;
	var username = req.body.username;
	var password = req.body.password;
	var password2 = req.body.password2;
});

module.exports = router;

1 个答案:

答案 0 :(得分:0)

第一件事:实际上,您在帖子('/ register'..)上什么都没做

您应该保存模型,如果成功,则将其返回,如下所示:

var schema = new mongoose.Schema({ name: 'string', email: 'string' });
var User = mongoose.model('User', schema);

//register user
router.post('/register', function(req, res) {
  var name = req.body.name;
  var email = req.body.email;

  var user = new User({ name: name,  email: email});
  user.save(function (err) {
    if (err) throw err;
    res.send(user);
  });
});

当然,您需要在单独的文件夹中定义模型,然后将其导入到适合的位置。