-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7ec4e9b
commit 152cccf
Showing
16 changed files
with
355 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
var express = require("express"); | ||
var path = require("path"); | ||
var app = express(); | ||
var router = require("./routes/index"); | ||
var bodyParser = require("body-parser"); | ||
|
||
app.use("/public", express.static(path.join(__dirname, "public"))); | ||
app.use("/node_modules", express.static(path.join(__dirname, "node_modules"))); | ||
|
||
|
||
app.engine("html", require("express-art-template")); | ||
|
||
app.set("views", path.join(__dirname, "./views")); | ||
|
||
app.get("/", function (req, res) { | ||
res.render("index.html"); | ||
}); | ||
|
||
app.get("/login", function (req, res) { | ||
res.render("login.html"); | ||
}); | ||
// parse application/x-www-form-urlencoded | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
// parse application/json | ||
app.use(bodyParser.json()); | ||
|
||
app.get("/register", function (req, res) { | ||
res.render("register.html"); | ||
}); | ||
app.use(router); | ||
|
||
app.listen(3000, () => console.log("running")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,61 @@ | ||
var mongoose = require('mongoose') | ||
var mongoose = require("mongoose"); | ||
|
||
// 连接数据库 | ||
mongoose.connect('mongodb://localhost/test', { useMongoClient: true }) | ||
mongoose.connect("mongodb://test:test123@localhost/test", { | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true, | ||
}); | ||
|
||
var Schema = mongoose.Schema | ||
var Schema = mongoose.Schema; | ||
|
||
var userSchema = new Schema({ | ||
email: { | ||
type: String, | ||
required: true | ||
required: true, | ||
}, | ||
nickname: { | ||
type: String, | ||
required: true | ||
required: true, | ||
}, | ||
password: { | ||
type: String, | ||
required: true | ||
required: true, | ||
}, | ||
created_time: { | ||
type: Date, | ||
// 注意:这里不要写 Date.now() 因为会即刻调用 | ||
// 这里直接给了一个方法:Date.now | ||
// 当你去 new Model 的时候,如果你没有传递 create_time ,则 mongoose 就会调用 default 指定的Date.now 方法,使用其返回值作为默认值 | ||
default: Date.now | ||
default: Date.now, | ||
}, | ||
last_modified_time: { | ||
type: Date, | ||
default: Date.now | ||
default: Date.now, | ||
}, | ||
avatar: { | ||
type: String, | ||
default: '/public/img/avatar-default.png' | ||
default: "/public/img/avatar-default.png", | ||
}, | ||
bio: { | ||
type: String, | ||
default: '' | ||
default: "", | ||
}, | ||
gender: { | ||
type: Number, | ||
enum: [-1, 0, 1], | ||
default: -1 | ||
default: -1, | ||
}, | ||
birthday: { | ||
type: Date | ||
type: Date, | ||
}, | ||
status: { | ||
type: Number, | ||
// 0 没有权限限制 | ||
// 1 不可以评论 | ||
// 2 不可以登录 | ||
enum: [0, 1, 2], | ||
default: 0 | ||
} | ||
}) | ||
default: 0, | ||
}, | ||
}); | ||
|
||
module.exports = mongoose.model('User', userSchema) | ||
module.exports = mongoose.model("User", userSchema); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
var express = require("express"); | ||
var sessionRouter = require("./session"); | ||
|
||
// 公共 | ||
var router = express.Router(); | ||
sessionRouter(router) | ||
|
||
router.get("/", function (req, res) { | ||
res.render("index.html"); | ||
}); | ||
|
||
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,55 @@ | ||
var md5 = require("blueimp-md5"); | ||
var User = require("../models/user"); | ||
|
||
// 注册、登陆、退出 | ||
module.exports = function (router) { | ||
router.get("/login", function (req, res) { | ||
res.render("login.html"); | ||
}); | ||
|
||
router.get("/register", function (req, res) { | ||
res.render("register.html"); | ||
}); | ||
|
||
router.post("/register", function (req, res) { | ||
var body = req.body; | ||
body.password = md5(md5(body.password)); | ||
new Promise(function (resolve, reject) { | ||
User.findOne({ email: body.email }, function (err, data) { | ||
if (err) throw err; | ||
return resolve(data); | ||
}); | ||
}) | ||
.then((data) => { | ||
if (data) { | ||
return res.status(200).json({ | ||
err_code: 1, | ||
message: "email already exists", | ||
}); | ||
} | ||
User.findOne({ nickname: body.nickname }, function (err, result) { | ||
if (err) throw err; | ||
if (result) { | ||
return res.status(200).json({ | ||
err_code: 2, | ||
message: "nickname already exists", | ||
}); | ||
} | ||
|
||
new User(body).save(function (error) { | ||
if (error) throw error; | ||
return res.status(200).json({ | ||
err_code: 0, | ||
message: "ok", | ||
}); | ||
}); | ||
}); | ||
}) | ||
.catch((err) => | ||
res.status(500).json({ | ||
err_code: 500, | ||
message: "Internal Error", | ||
}) | ||
); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.