Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
NorthWood
FoxTeam
MEVN
提交
465f5a37
M
MEVN
项目概览
NorthWood
/
FoxTeam
/
MEVN
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
MEVN
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
465f5a37
编写于
8月 25, 2021
作者:
J
Jeff Fox
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
REST APIs
上级
6cb22183
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
73 addition
and
20 deletion
+73
-20
express_app/app.js
express_app/app.js
+14
-13
express_app/controllers/users.js
express_app/controllers/users.js
+56
-4
express_app/models/User.js
express_app/models/User.js
+3
-3
未找到文件。
express_app/app.js
浏览文件 @
465f5a37
...
...
@@ -3,6 +3,10 @@ var express = require('express');
var
path
=
require
(
'
path
'
);
var
cookieParser
=
require
(
'
cookie-parser
'
);
var
logger
=
require
(
'
morgan
'
);
// Require file system module
var
fs
=
require
(
'
file-system
'
);
var
mongoose
=
require
(
'
mongoose
'
);
var
app
=
express
();
...
...
@@ -15,17 +19,6 @@ mongoose.connect('mongodb://localhost:27017/express_app', function(){
process
.
exit
(
1
);
});
// Require file system module
var
fs
=
require
(
'
file-system
'
);
// Include controllers
fs
.
readdirSync
(
'
controllers
'
).
forEach
(
function
(
file
)
{
if
(
file
.
substr
(
-
3
)
==
'
.js
'
)
{
const
route
=
require
(
'
./controllers/
'
+
file
)
route
.
controller
(
app
)
}
})
// view engine setup
app
.
set
(
'
views
'
,
path
.
join
(
__dirname
,
'
views
'
));
app
.
set
(
'
view engine
'
,
'
pug
'
);
...
...
@@ -36,6 +29,14 @@ app.use(express.urlencoded({ extended: false }));
app
.
use
(
cookieParser
());
app
.
use
(
express
.
static
(
path
.
join
(
__dirname
,
'
public
'
)));
// Include controllers
fs
.
readdirSync
(
'
controllers
'
).
forEach
(
function
(
file
)
{
if
(
file
.
substr
(
-
3
)
==
'
.js
'
)
{
const
route
=
require
(
'
./controllers/
'
+
file
)
route
.
controller
(
app
)
}
})
// catch 404 and forward to error handler
app
.
use
(
function
(
req
,
res
,
next
)
{
next
(
createError
(
404
));
...
...
@@ -52,6 +53,6 @@ app.use(function(err, req, res, next) {
res
.
render
(
'
error
'
);
});
app
.
listen
(
3000
,
function
(){
console
.
log
(
'
listening on 3000
'
)})
module
.
exports
=
app
;
app
.
listen
(
3000
,
function
(){
console
.
log
(
'
listening on 3000
'
)})
\ No newline at end of file
express_app/controllers/users.js
浏览文件 @
465f5a37
var
User
=
require
(
'
../models/User
'
);
module
.
exports
.
controller
=
(
app
)
=>
{
// get users
// get all users
app
.
get
(
'
/users
'
,
(
req
,
res
)
=>
{
res
.
render
(
'
users
'
,
{
title
:
'
Users
'
,
description
:
'
This is the description of all users
'
});
User
.
find
({},
'
name email
'
,
function
(
error
,
users
)
{
if
(
error
){
console
.
log
(
error
);
}
res
.
send
({
users
:
users
})
})
})
// get a single user details
app
.
get
(
'
/users/:id
'
,
(
req
,
res
)
=>
{
User
.
findById
(
req
.
params
.
id
,
'
name email
'
,
function
(
error
,
user
){
if
(
error
){
console
.
log
(
error
);
}
res
.
send
(
user
)
})
})
// add a new user
app
.
post
(
'
/users
'
,
(
req
,
res
)
=>
{
const
user
=
new
User
({
name
:
req
.
body
.
name
,
email
:
req
.
body
.
email
})
user
.
save
(
function
(
error
,
user
){
if
(
error
){
console
.
log
(
error
);
}
res
.
send
(
user
)
})
})
// update a user
app
.
put
(
'
/users/:id
'
,
(
req
,
res
)
=>
{
User
.
findById
(
req
.
params
.
id
,
'
name email
'
,
function
(
error
,
user
){
if
(
error
)
{
console
.
log
(
error
);
}
user
.
name
=
req
.
body
.
name
user
.
email
=
req
.
body
.
email
user
.
save
(
function
(
error
,
user
){
if
(
error
){
console
.
log
(
error
);
}
res
.
send
(
user
)
})
})
})
// delete a user
app
.
delete
(
'
/users/:id
'
,
(
req
,
res
)
=>
{
User
.
remove
({
_id
:
req
.
params
.
id
},
function
(
error
,
user
){
if
(
error
){
console
.
error
(
error
);
}
res
.
send
({
success
:
true
})
})
})
}
\ No newline at end of file
express_app/models/User.js
浏览文件 @
465f5a37
const
mo
goose
=
require
(
'
mongoose
'
);
const
mo
ngoose
=
require
(
'
mongoose
'
)
const
Schema
=
mongoose
.
Schema
;
const
Schema
=
mongoose
.
Schema
const
UserSchema
=
new
Schema
({
name
:
String
,
...
...
@@ -8,4 +8,4 @@ const UserSchema = new Schema({
})
const
User
=
mongoose
.
model
(
'
User
'
,
UserSchema
)
module
.
expose
=
User
\ No newline at end of file
module
.
exports
=
User
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录