目前,我已经成功集成了路由,请求和配置。
app.js-我们应用程序的入口点。该文件定义了我们的Express服务器,并使用mongoose将其连接到MongoDB。它还需要我们将在应用程序中使用的路线和模型。
config /-此文件夹包含护照的配置以及配置/环境变量的中央位置。
routes /-此文件夹包含我们API的路由定义。
models /-此文件夹包含我们的Mongoose模型的架构定义。
routes / api / users.js
var mongoose = require('mongoose');
var router = require('express').Router();
var passport = require('passport');
var User = mongoose.model('User');
var auth = require('../auth');
router.post('/users', function(req, res, next){
var user = new User();
user.username = req.body.user.username;
user.email = req.body.user.email;
user.setPassword(req.body.user.password);
user.save(req, res).then(function(){
return res.json({user: user.toAuthJSON()});
}).catch(next);
});
module.exports = router;
模型/User.js
var mongoose = require('mongoose');
var uniqueValidator = require('mongoose-unique-validator');
var crypto = require('crypto');
var jwt = require('jsonwebtoken');
var secret = require('../config').secret;
var UserSchema = new mongoose.Schema({
username: {type: String, lowercase: true, unique: true, required: [true, "can't be blank"], match: [/^[a-zA-Z0-9]+$/, 'is invalid'], index: true},
email: {type: String, lowercase: true, unique: true, required: [true, "can't be blank"], match: [/\S+@\S+\.\S+/, 'is invalid'], index: true},
bio: String,
image: String,
favorites: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Article' }],
following: [{ type: mongoose.Schema.Types.ObjectId, ref: 'User' }],
hash: String,
salt: String
}, {timestamps: true});
/**<<HERE>> Does not work inside the models. <<HERE>>**/
UserSchema.plugin(uniqueValidator, {message: i18n.__('user.register.errors.is_already_taken')});
mongoose.model('User', UserSchema);
app.js
var http = require('http'),
path = require('path'),
methods = require('methods'),
express = require('express'),
bodyParser = require('body-parser'),
session = require('express-session'),
cors = require('cors'),
passport = require('passport'),
errorhandler = require('errorhandler'),
mongoose = require('mongoose');
cookieParser = require('cookie-parser');
i18n = require('i18n');
var isProduction = process.env.NODE_ENV === 'production';
// Create global app object
var app = express();
app.use(cors());
app.use(cookieParser())
// Configure i18n
i18n.configure({
locales:['en', 'es'],
directory: __dirname + '/locales',
objectNotation: true,
//defaultLocale: 'en',
register: global,
cookie: 'lang'
})
i18n.init();
// you will need to use cookieParser to expose cookies to req.cookies
app.use(cookieParser());
// i18n init parses req for language headers, cookies, etc.
app.use(i18n.init);
// Support "Accept-Language" : "es" or "en" via headers
app.use(function (req, res, next) {
i18n.init(req, res);
if (typeof req.locale !== 'undefined') {
console.log('locale : ', req.locale);
i18n.setLocale(req.locale);
}else{
var locale = i18n.getLocale();
res.set('Content-Language', locale);
}
next();
});
// finally, let's start our server...
var server = app.listen( process.env.PORT || 3000, function(){
console.log('Listening on port ' + server.address().port);
});
但是当我尝试在 models / 中进行翻译时,例如i18n .__('my-key'),它不会检测到语言环境,因此会返回默认“ en”的语言环境,但是从标头中我可以正确收到lang“ es”。
请让我知道我将如何解决它:)