const express = require('express')
const app = express()
const MongoClient = require('mongodb').MongoClient
app.set('view engine', 'ejs')
app.engine('ejs',require('ejs').__express)
app.use(express.urlencoded({ extended: true }));
var db;
MongoClient.connect('mongodb+srv://admin:admin1234!@cluster0.unkjl34.mongodb.net/?retryWrites=true&w=majority',
function(에러, client){
if (에러) return console.log(에러)
db=client.db('todoapp')
db.collection('post').insertOne({이름:'Park',_id:100},function(에러,client){
console.log('저장완료')
});
app.listen(8081, function() {
console.log('listening on 8081') })})
//여기 이하는 쓸데없는 app.get 이런 코드들
app.get('/', function(요청, 응답) {
응답.sendFile(__dirname +'/index.html')})
// 누군가가 /pet으로 방문을 하면// pet 관련 안내문을 띄워주자
app.get('/pet',function(요청,응답){
응답.send('펫 용품 쇼핑할 수 있는 페이지 입니다.')});
app.get('/beauty',function(요청,응답){
응답.send('뷰티 용품 쇼핑할 수 있는 페이지 입니다.')});
app.get('/write',function(요청,응답){
응답.sendFile(__dirname + '/write.html')});
app.get('/list',function(요청,응답){
db.collection('post').find().toArray(function(에러, 결과){
console.log(결과)
응답.render('list.ejs',{posts:결과});
});
//디비에 저장된 post라는 collection안의 모든 데이터를 꺼내주세요
app.post('/add', function(요청, 응답){
응답.send('전송완료');
db.collection('post').insertOne( { 제목 : 요청.body.title, 날짜 : 요청.body.date } , function(에러,결과){
console.log('저장완료')
});
});