我正在研究锁定Angular路由(https://catalin.me/role-based-authentication-with-angular-express-jwt-mysql-part-1/)的教程,但遇到一个错误:“从v4.0.0开始,必须明确提供方言”。我已经阅读了SO以及Web上其他地方的各种文档,但是这些建议似乎都不适用于此实例。有什么建议吗?
我的代码如下:
config.js(请注意方言在第17行)
// app/config.js
// Application configuration.
'use strict';
var config = module.exports;
config.db = {
user: 'username',
password: 'password',
name: 'users'
};
config.db.details = {
host: 'mysaldatabase',
port: 3306,
dialect: 'mysql'
};
config.keys = {
secret: '/jVdfUX+u/Kn3qPY4+ahjwQgyV5UhkM5cdh1i2xhozE=' // Not anymore...
};
database.js(这是新的Sequelize发挥作用的地方)
// app/services/database.js
'use strict';
var config = require('./../config'),
Sequelize = require('sequelize');
module.exports = new Sequelize(
config.db.id,
config.db.user,
config.db.password,
config.db.email
);
user.js
// app/models/user.js
// The User model.
"use strict";
var Sequelize = require("sequelize"),
bcrypt = require("bcrypt");
var config = require("../config"),
db = require("../services/database");
// 1: The model schema.
var modelDefinition = {
username: {
type: Sequelize.STRING,
unique: true,
allowNull: false
},
password: {
type: Sequelize.STRING,
allowNull: false
}
};
// 2: The model options.
var modelOptions = {
instanceMethods: {
comparePasswords: comparePasswords
},
hooks: {
beforeValidate: hashPassword
}
};
// 3: Define the User model.
var UserModel = db.define("user", modelDefinition, modelOptions);
// Compares two passwords.
function comparePasswords(password, callback) {
bcrypt.compare(password, this.password, function(error, isMatch) {
if (error) {
return callback(error);
}
return callback(null, isMatch);
});
}
// Hashes the password for a user object.
function hashPassword(user) {
if(user.changed('password')) {
return bcrypt.hash(user.password, 10).then(function(password) {
user.password = password;
});
}
}
module.exports = UserModel;
server.js
'use strict';
// 1: NPM dependencies.
var express = require('express'),
bodyParser = require('body-parser'),
morgan = require('morgan'),
sequelize = require('sequelize'),
passport = require('passport'),
jwt = require('jsonwebtoken');
// 2: App related modules.
var hookJWTStrategy = require('./services/passportStrategy');
// 3: Initializations.
var app = express();
// 4: Parse as urlencoded and json.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// 5: Hook up the HTTP logger.
app.use(morgan('dev'));
// 6: Hook up Passport.
app.use(passport.initialize());
// Hook the passport JWT strategy.
hookJWTStrategy(passport);
// 7: Set the static files location.
app.use(express.static(__dirname + '../../public'));
// 8: Home route.
app.get('/', function(req, res) {
res.send('Nice meeting you wizard, I\'m Gandalf!');
});
// 9: Start the server.
app.listen('8080', function() {
console.log('Magic happens at http://localhost:8080/! We are all now doomed!');
});
passportStrategy.js
'use strict';
// 1: NPM dependencies.
var express = require('express'),
bodyParser = require('body-parser'),
morgan = require('morgan'),
sequelize = require('sequelize'),
passport = require('passport'),
jwt = require('jsonwebtoken');
// 2: App related modules.
var hookJWTStrategy = require('./services/passportStrategy');
// 3: Initializations.
var app = express();
// 4: Parse as urlencoded and json.
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
// 5: Hook up the HTTP logger.
app.use(morgan('dev'));
// 6: Hook up Passport.
app.use(passport.initialize());
// Hook the passport JWT strategy.
hookJWTStrategy(passport);
// 7: Set the static files location.
app.use(express.static(__dirname + '../../public'));
// 8: Home route.
app.get('/', function(req, res) {
res.send('Nice meeting you wizard, I\'m Gandalf!');
});
// 9: Start the server.
app.listen('8080', function() {
console.log('Magic happens at http://localhost:8080/! We are all now doomed!');
});