정승호

post 기능 추가

1 1
2 const express = require('express') 2 const express = require('express')
3 const app = express() 3 const app = express()
4 +const bodyParser = require('body-parser');
4 const port = 5000 5 const port = 5000
5 6
7 +const { User } = require('/models/Users');
8 +
9 +app.use(bodyParser.urlencoded({extended : true}));
10 +app.use(bodyParser.json());
11 +
12 +
6 const mongoose = require('mongoose') 13 const mongoose = require('mongoose')
7 mongoose.connect('mongodb+srv://platoon07:wony9795!!@2020oss-ysxss.mongodb.net/test?retryWrites=true&w=majority',{ 14 mongoose.connect('mongodb+srv://platoon07:wony9795!!@2020oss-ysxss.mongodb.net/test?retryWrites=true&w=majority',{
8 useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex : true, useFindAndModify: false 15 useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex : true, useFindAndModify: false
...@@ -12,7 +19,22 @@ mongoose.connect('mongodb+srv://platoon07:wony9795!!@2020oss-ysxss.mongodb.net/t ...@@ -12,7 +19,22 @@ mongoose.connect('mongodb+srv://platoon07:wony9795!!@2020oss-ysxss.mongodb.net/t
12 19
13 20
14 app.get('/', (req,res) => res.send('Hello world!!')) 21 app.get('/', (req,res) => res.send('Hello world!!'))
15 -app.listen(port, () => console.log('example app listen on port ${port} !')) 22 +
23 +app.post('/register', (req, res) => {
24 + // 회원 가입시 필요한 정보들을 client에서 가져오면
25 + // 그것들을 데이터베이스에 넣어준다.
26 + const user = new User(req.body)
27 +
28 + user.save((err, userInfo) => {
29 + if(err) return res.json({success : false, err})
30 + return res.status(200).json({
31 + success : true
32 + })
33 + }) // MongoDb에 저징
34 +})
35 +
36 +
37 +app.listen(port, () => console.log('example app listen on port ${port}!'))
16 38
17 39
18 // mongodb+srv://platoon07:<password>@2020oss-ysxss.mongodb.net/test?retryWrites=true&w=majority 40 // mongodb+srv://platoon07:<password>@2020oss-ysxss.mongodb.net/test?retryWrites=true&w=majority
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
14 "author": "seungho", 14 "author": "seungho",
15 "license": "ISC", 15 "license": "ISC",
16 "dependencies": { 16 "dependencies": {
17 + "body-parser": "^1.19.0",
17 "express": "^4.17.1", 18 "express": "^4.17.1",
18 "mongoose": "^5.9.15" 19 "mongoose": "^5.9.15"
19 } 20 }
......