Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
students
git example
Commits
176c0757
Commit
176c0757
authored
Jan 13, 2023
by
이다현
Browse files
1월 13일 수업 예제 작성
parent
0dbb4769
Changes
3
Show whitespace changes
Inline
Side-by-side
src/app.js
0 → 100644
View file @
176c0757
import
express
from
"
express
"
;
// 웹서버 관련된 모듈
import
User
from
"
./models/users.model.js
"
;
const
app
=
express
();
app
.
get
(
"
/hello
"
,
(
req
,
res
)
=>
{
console
.
log
(
"
baseUrl
"
,
req
.
baseUrl
,
"
hostname
"
,
req
.
hostname
,
"
url
"
,
req
.
url
);
console
.
log
(
"
root path get method called.
"
);
res
.
send
(
"
<h1>안녕하세요.</h1>
"
);
// res.json({ message: "안녕", id: 12 });
});
// http://localhost:3000/hello 가 실행된다
app
.
get
(
"
/users
"
,
async
(
req
,
res
)
=>
{
const
newUser
=
await
User
.
create
({
password
:
"
xkkxkxk
"
,
email
:
"
xxx@xxxx.com
"
,
name
:
"
dkxkdkf
"
,
});
console
.
log
(
"
new user:
"
,
newUser
);
const
users
=
await
User
.
find
();
res
.
json
(
users
);
});
// http://localhost:3000/users 가 실행된다
export
default
app
;
// app.listen(port, () => {
// console.log(`server is running at ${port} port.`);
// });
src/index.js
View file @
176c0757
import
express
from
"
express
"
;
import
mongoose
from
"
mongoose
"
;
import
app
from
"
./app.js
"
;
const
port
=
3000
;
const
app
=
express
();
app
.
get
(
"
/api/hello
"
,
(
req
,
res
)
=>
{
console
.
log
(
"
baseUrl
"
,
req
.
baseUrl
,
"
hostname
"
,
req
.
hostname
,
"
url
"
,
req
.
url
);
console
.
log
(
"
root path get method called.
"
);
res
.
send
(
"
<h1>안녕하세요.</h1>
"
);
// res.json({ message: "안녕", id: 12 });
});
app
.
listen
(
port
,
()
=>
{
mongoose
.
connect
(
"
mongodb://127.0.0.1:27017/test
"
)
// uri: mongodb 연결하는 주소(문자열) -> mongodb://localhost:27017/test / host:port 번호 / test: 데이터베이스 이름
.
then
((
mgs
)
=>
{
console
.
log
(
`버전
${
mgs
.
version
}
몽구스: 몽고디비가 접속 되었습니다.`
);
app
.
listen
(
port
,
()
=>
{
console
.
log
(
`server is running at
${
port
}
port.`
);
});
});
// 웹서버를 불러옴
})
.
catch
((
error
)
=>
{
console
.
log
(
error
);
});
// 에러를 잡아줌
src/models/users.model.js
0 → 100644
View file @
176c0757
import
{
Schema
,
model
}
from
"
mongoose
"
;
const
validateEmail
=
(
email
)
=>
{
const
re
=
/^
\w
+
([\.
-
]?\w
+
)
*@
\w
+
([\.
-
]?\w
+
)
*
(\.\w{2,3})
+$/
;
return
re
.
test
(
email
);
};
const
schema
=
new
Schema
({
// Schema는 데이터를 만드는 틀
email
:
{
// email 이라는 column을 만듦, email에 대한 옵션 설정
type
:
String
,
// type은 항상 지정해야 함
required
:
true
,
// email이 꼭 필요하다는 의미로, email을 입력하지 않으면 에러 발생
unique
:
true
,
// 이메일이 중복되면 안 된다는 의미로, 중복 입력시 에러 발생
validate
:
[
validateEmail
,
"
이메일을 입력해주세요
"
],
// true이면 validateEmail을, false이면 "이메일을 입력해주세요"을 나타냄
},
name
:
{
type
:
String
},
// name 이라는 column을 만듦
password
:
{
type
:
String
,
required
:
true
,
select
:
false
},
// password 라는 column을 만듦
role
:
{
type
:
Schema
.
Types
.
ObjectId
,
ref
:
"
Role
"
},
// role 이라는 column을 만듦
});
export
default
model
(
"
User
"
,
schema
);
// 변수 schema를 User라는 이름으로 내보낸다
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment