0
人がアカウントを作成したときにデータベースを埋めようとしています。私のサーバーはnode.jsにあります。 app.js:フォームのデータエントリからmysqlデータベースを埋める方法は?
var http=require("http");
var express = require('express');
var app = express();
var server = http.createServer(app);
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var session = require('express-session');
var bcrypt = require('bcrypt-nodejs');
var ejs = require('ejs');
var path = require('path');
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
// routes
var route = require('./route');
// model
var Model = require('./model');
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(cookieParser());
app.use(bodyParser.urlencoded({extended:false}));
app.use(session({secret: 'secret strategic xxzzz code',
resave:false,
saveUninitialized:false
}));
app.use(passport.initialize());
app.use(passport.session());
server.listen(3000,function(){
console.log("server listen at localhost:3000");});
passport.use(new LocalStrategy(function(username, password, done) {
new Model.User({username: username}).fetch().then(function(data) {
var user = data;
if(user === null) {
return done(null, false, {message: 'Invalid username or password'});
} else {
user = data.toJSON();
if(!bcrypt.compareSync(password, user.password)) {
return done(null, false, {message: 'Invalid username or password'});
} else {
return done(null, user);
}
}
});
}));
passport.serializeUser(function(user, done) {
done(null, user.username);
});
passport.deserializeUser(function(username, done) {
new Model.User({username: username}).fetch().then(function(user) {
done(null, user);
});
});
// signup
// GET
app.get('/signup', route.signUp);
// POST
app.post('/signup', route.signUpPost);
route.js:
var signUpPost = function(req, res, next) {
var user = req.body;
var usernamePromise = null;
usernamePromise = new Model.User({username: user.username}).fetch();//verify the existence of username on the database
return usernamePromise.then(function(model) {
if(model) {
res.render('index1', {title: 'Index', errorMessage: 'username already exists'});//verify if the entered username exists in the databas.
} else {
var password = user.password;
var email=user.email;
if(password.length<5){
res.render('index1',{title:'index',errorMessage:'password so weak'});//password must been > 5 caracters.
}
else if(verifMail(email) === false){
res.render('index1',{title:'index',errorMessage:'email invalid'});//if the email is not valid,it show an error
}
else{
var hash = bcrypt.hashSync(password);//crypt the password
var signUpUser = new Model.User({username: user.username, password:hash,email:user.email,firstname:user.firstname,lastname:user.lastname});//add a new row in the database
signUpUser.save().then(function(model) {
signInPost(req, res, next);
});
};
}
});
};
Model.js:
var DB = require('./db').DB;
var User = DB.Model.extend({
tableName: 'newuser',
idAttribute: 'userId',
});
module.exports = {
User: User
};
db.js:
var Bookshelf = require('bookshelf');
var config = {
host: 'localhost',
user: 'root',
password: '',
database: 'Users',
charset: 'UTF8_GENERAL_CI'
};
var DB = Bookshelf.initialize({
client: 'mysql',
connection: config
});
module.exports.DB = DB;
人がクリックサインアップするためのボタン、最初に入力するモーダル表示名前、姓、電子メール、パスワード。
<div id="myModal" class="modal fade" role="dialog" >
<div class="model-content">
<div class="modal-header" >
<button type="button" class="close" data-dismiss="modal">×</button>
<h3 id="myModalLabel">Modal header</h3>
</div>
<div class="modal-body">
<!-- form -->
<form id="register-form" role="form" method="post" action="/signup">
<div class="form-group">
<input type="text" class="form-control" id="username" placeholder="User name">
</div>
<div class="form-group">
<input type="text" class="form-control" id="firstname" placeholder="First name">
</div>
<div class="form-group">
<input type="text" class="form-control" id="lastname" placeholder="Last name">
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" placeholder="Email">
</div>
<div class="form-group">
<input type="password" class="form-control" id="password" placeholder="Password">
</div>
<div class="emaillogin">
<button type="submit" class="btn btn-default email-signin-button">
Submit
</button>
</div>
</form>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-default email-signin-button" data-dismiss="modal">
Close
</button>
</div>
</div>
</div>
ページはまだ長時間リロードされ、データはデータベースに保存されませんでした。