
connect 밑에 위치할경우 정상적으로 출력이 되지만
이 코드 가장 아래에 위치할 경우에는 제대로 출력이 되지 않습니다
이유가 뭔가요?
const express = require('express');
const app = express();
const bodyParser = require('body-parser')
app.use(bodyParser.urlencoded({ extended: true }))
const MongoClient = require('mongodb').MongoClient;
app.set('view engine', 'ejs');
var db;
MongoClient.connect('mongodb+srv://greatcloud13:tmdghks8010@cluster0.twp1okv.mongodb.net/?retryWrites=true&w=majority', function (err, client) {
if (err) return console.log(err)
db = client.db('todoapp');
app.listen(8080, function () {
console.log('listening on 8080')
})
});
app.get('/list', function(요청, 응답){
db.collection('post').find().toArray(function(에러, 결과){
console.log(결과)
응답.render('list.ejs', { posts : 결과 })
})
})
app.get('/pet', function (req, res) {
res.send('Pet toy page')
});
app.get('/beauty', (req, res) => {
res.send('Beauty page')
});
app.get('/', function (req, res) {
res.sendFile(__dirname + '/index.html')
});
app.get('/write', function (req, res) {
res.sendFile(__dirname + '/write.html')
});
app.get('/list', function (req, res) {
res.render('list.ejs')
});
app.post('/add', function (req, res) {
res.send('Send complete')
db.collection('post').insertOne({ name: req.body.title, date:req.body.date }, function (err, result) {
console.log('Save complete')
});
});
코드도 함께 첨부합니다