Skip to content

Commit 0a0dd08

Browse files
committed
add api prefix
1 parent 7a652f6 commit 0a0dd08

File tree

6 files changed

+100
-87
lines changed

6 files changed

+100
-87
lines changed

app/kernel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ require("./passport");
7777

7878
/* Serving api routes */
7979

80-
require("./routes/api");
80+
app.use("/" + _config.app.api_prefix, require("./routes/api"));
8181

8282
/* Serving web routes */
8383

84-
require("./routes/web");
84+
app.use("/", require("./routes/web"));
8585

8686
/* 404 error handler */
8787

app/models/User.js

Lines changed: 72 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,93 @@
1+
var mongoose = require("mongoose");
12
var bcrypt = require("bcrypt");
23

3-
module.exports = function (mongoose) {
4-
5-
var schema = mongoose.Schema({
6-
username: {
7-
type: String,
8-
unique: true
9-
},
10-
email: {
11-
type: String,
12-
required: true,
13-
unique: true
14-
},
15-
password: {
16-
type: String,
17-
required: true
18-
},
19-
first_name: {
20-
type: String,
21-
required: true
22-
},
23-
score: {
24-
type: Number,
25-
default: 0
26-
},
27-
lang: {
28-
type: String,
29-
default: 'en'
30-
},
31-
created_at: {
32-
type: Date,
33-
default: Date.now
34-
},
35-
updated_at: {
36-
type: Date,
37-
default: Date.now
38-
}
39-
40-
}, {
41-
versionKey: false,
42-
timestamps: true
4+
var schema = mongoose.Schema({
5+
username: {
6+
type: String,
7+
unique: true
8+
},
9+
email: {
10+
type: String,
11+
required: true,
12+
unique: true
13+
},
14+
password: {
15+
type: String,
16+
required: true
17+
},
18+
first_name: {
19+
type: String,
20+
required: true
21+
},
22+
score: {
23+
type: Number,
24+
default: 0
25+
},
26+
lang: {
27+
type: String,
28+
default: 'en'
29+
},
30+
created_at: {
31+
type: Date,
32+
default: Date.now
33+
},
34+
updated_at: {
35+
type: Date,
36+
default: Date.now
4337
}
44-
);
4538

46-
schema.pre('save', function (next) {
39+
}, {
40+
versionKey: false,
41+
timestamps: true
42+
}
43+
);
4744

48-
var user = this;
45+
schema.pre('save', function (next) {
4946

50-
// generate a salt
47+
var user = this;
5148

52-
if(user.isModified("password") || user.isNew) {
49+
// generate a salt
5350

54-
bcrypt.genSalt(10, function (error, salt) {
51+
if (user.isModified("password") || user.isNew) {
5552

56-
if (error) return next(error);
53+
bcrypt.genSalt(10, function (error, salt) {
5754

58-
// hash the password along with our new salt
55+
if (error) return next(error);
5956

60-
bcrypt.hash(user.password, salt, function (error, hash) {
57+
// hash the password along with our new salt
6158

62-
if (error) return next(error);
59+
bcrypt.hash(user.password, salt, function (error, hash) {
60+
61+
if (error) return next(error);
6362

64-
// override the cleartext password with the hashed one
63+
// override the cleartext password with the hashed one
6564

66-
user.password = hash;
65+
user.password = hash;
6766

68-
next(null, user);
69-
});
67+
next(null, user);
7068
});
69+
});
7170

72-
}else{
73-
next(null, user);
71+
} else {
72+
next(null, user);
73+
}
74+
});
75+
76+
/**
77+
* Compare raw and encrypted password
78+
* @param password
79+
* @param callback
80+
*/
81+
schema.methods.comparePassword = function (password, callback) {
82+
bcrypt.compare(password, this.password, function (error, match) {
83+
if (error) callback(error);
84+
if (match) {
85+
callback(null, true);
86+
} else {
87+
callback(error, false);
7488
}
7589
});
90+
}
7691

77-
/**
78-
* Compare raw and encrypted password
79-
* @param password
80-
* @param callback
81-
*/
82-
schema.methods.comparePassword = function (password, callback) {
83-
bcrypt.compare(password, this.password, function (error, match) {
84-
if (error) callback(error);
85-
if (match) {
86-
callback(null, true);
87-
} else {
88-
callback(error, false);
89-
}
90-
});
91-
}
92+
module.exports = mongoose.model("user", schema, "user");
9293

93-
return mongoose.model("user", schema, "user");
94-
};

app/routes/api.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
app.post("/api/token", app.controllers.AuthController.token);
2-
app.get("/user", app.controllers.UserController.find);
3-
app.get("/user/:id", app.controllers.UserController.findOne);
4-
app.post("/user", app.controllers.UserController.create);
5-
app.put("/user/:id", app.controllers.UserController.update);
6-
app.delete("/user/:id", app.controllers.UserController.destroy);
1+
var router = require("express").Router();
72

3+
router.post("/token", app.controllers.AuthController.token);
4+
5+
router.get("/user", app.controllers.UserController.find);
6+
router.get("/user/:id", app.controllers.UserController.findOne);
7+
router.post("/user", app.controllers.UserController.create);
8+
router.put("/user/:id", app.controllers.UserController.update);
9+
router.delete("/user/:id", app.controllers.UserController.destroy);
10+
11+
module.exports = router;

app/routes/web.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
app.get(
1+
var router = require("express").Router();
2+
3+
router.get(
24
"/",
35
HomeController.index
46
);
57

6-
app.get(
8+
router.get(
79
"/logout",
810
AuthController.logout
911
);
1012

11-
app.get(
13+
router.get(
1214
"/profile",
1315
SessionAuth,
1416
HomeController.profile
1517
);
18+
19+
module.exports = router;

config/app.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ module.exports = {
3535
* The views directory path
3636
*/
3737

38-
views: require('path').join(__basepath, 'app/views')
38+
views: require('path').join(__basepath, 'app/views'),
39+
40+
41+
/**
42+
* The api url prefix
43+
*/
44+
45+
api_prefix: "api"
3946
};
4047

libs/helpers.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ var path = require("path");
22
var fs = require("fs");
33
var walkSync = require("./walkSync");
44
var merge = require("./merge");
5-
var mongoose = require("mongoose");
65

76
app.loadConfig = function () {
87

@@ -58,7 +57,7 @@ app.loadModels = function () {
5857
var models = {};
5958

6059
files.forEach(function (file) {
61-
models[file] = require(path.join(directory, file))(mongoose);
60+
models[file] = require(path.join(directory, file));
6261
});
6362

6463
if (_config.globals.models) {

0 commit comments

Comments
 (0)