
const express = require('express');
const app = express();
const bodyParser= require('body-parser');
app.use(bodyParser.urlencoded({extended: true}));
const MongoClient = require('mongodb').MongoClient;
var db;
MongoClient.connect('mongodb+srv://admin:1111@cluster0.5jnwt.mongodb.net/myFirstDatabase?retryWrites=true&w=majority', { useUnifiedTopology: true }, function (에러, client) {
if (에러) return console.log(에러)
db = client.db('todoapp');
db.collection('post').insertOne( {이름 : 'John', 나이: 100} , function(에러, 결과){
console.log('저장완료');
});
app.listen(8080, function () {
console.log('listening on 8080')
});
});
app.get('/', function(요청, 응답) {
응답.sendFile(__dirname +'/index.html');
})
app.get('/write', function(요청, 응답) {
응답.sendFile(__dirname +'/write.html');
});
app.post('/add', function(요청, 응답){
console.log(요청.body);
응답.send('전송완료')
});
app.post('/add', function(요청, 응답){
응답.send('전송완료');
console.log(요청.body.title);
console.log(요청.body.date);
db.collection('post').insertOne( { 제목 : 요청.body.title, 날짜 : 요청.body.date } , function(에러, 결과){
console.log('저장완료')
});
});


-----------------------------------------------------------------------------------------------------------------------------------------
제가 몽고db에 연결하고 있는데 위에 선언문은 잘 저장이 되는데 왜 아래 있는
폼에서는 저장이 안되는지 모르겠습니다. 콘솔에도 정상적으로 뜨는데 데이터가 저장이 안되네요 터미널에는 잘 뜹니다.
db연결도 잘 되었고 form에 name 요소도 모두 적용했습니다
app.post('/add', function(요청, 응답){
응답.send('전송완료');
console.log(요청.body.title);
console.log(요청.body.date);
db.collection('post').insertOne( { 제목 : 요청.body.title, 날짜 : 요청.body.date } , function(에러, 결과){
console.log('저장완료')
});
});