4 글 보임 - 1 에서 4 까지 (총 4 중에서)
-
글쓴이글
-
2022년 7월 14일 10:28 #38654
강호문참가자로그인 후 write.를 했습니다. DB collection에 저장완료 되는데 그 다음 listening 8080 안 넘어가고 write.ejs 에서 무한루프를 도는 불량입니다. 에러메시지는 Unchecked runtime.lastError: The message port closed before a response was received. 가 나옵니다. 해결책 부탁드립니다~~~
2022년 7월 15일 06:18 #38728
강호문참가자server.js가 아래와 같이 돼 있는데 어느 위치가 문제인지 검토해 주시면 고맙겠습니다~~ (server.js) const express = require('express'); const app = express(); const bodyParser= require('body-parser') app.use(bodyParser.urlencoded({extended: true})) //const { MongoClient } = require('mongodb'); const MongoClient = require('mongodb').MongoClient;
app.set('view engine', 'ejs');
const methodOverride = require('method-override') app.use(methodOverride('_method'))
app.use('/public', express.static('public')) var db; MongoClient.connect('mongodb+srv://@cluster0.nhkdn.mongodb.net/myFirstDatabase?retryWrites=true&w=majority', function(에러,client){ if(에러) return console.log(에러) db=client.db('todoapp');
app.listen(8080, function() { console.log('listening on 8080') }); }); app.get('/', function(요청, 응답) { 응답.render('index.ejs') });
app.get('/list', function(요청, 응답){ db.collection('post').find().toArray(function(에러, 결과){ console.log(결과) 응답.render('list.ejs', {posts:결과}) }) })
app.delete('/delete' , function(요청, 응답){ console.log(요청.body); 요청.body._id = parseInt(요청.body._id); var 삭제할데이터={_id:요청.body._id, 작성자 : 요청.user._id}
db.collection('post').deleteOne(삭제할데이터, function(에러,결과){ console.log('삭제완료'); 응답.status(200).send({ message: '성공했어요'}) }) }) app.get('/edit/:id', function(요청, 응답){ db.collection('post').findOne({ _id : parseInt(요청.params.id) }, function(에러, 결과){ 응답.render('edit.ejs', { post : 결과 }) }) }); app.put('/edit', function(요청, 응답){ db.collection('post').updateOne( {_id : parseInt(요청.body.id)}, {$set : { 제목 : 요청.body.title , 날짜 : 요청.body.date }}, function(){ console.log('수정완료') 응답.redirect('/list') }); });
const passport = require('passport'); const LocalStrategy = require('passport-local').Strategy; const session = require('express-session');
app.use(session({secret : '0327', resave : true, saveUninitialized: false})); app.use(passport.initialize()); app.use(passport.session());
app.get('/login', function(요청, 응답){ 응답.render('login.ejs') });
app.post('/login', passport.authenticate('local', {failureRedirect : '/fail'}), function(요청, 응답){ 응답.redirect('/') });
app.get('/mypage', 로그인했니, (req, res) => { console.log(req.user); res.render('mypage.ejs', { 사용자: req.user}) })
function 로그인했니(req, res, next) { if (req.user) { next() } else { res.send('로그인 안하셨는데요?') } }
passport.use(new LocalStrategy({ usernameField: 'id', passwordField: 'pw', session: true, passReqToCallback: false, }, function (입력한아이디, 입력한비번, done) { //console.log(입력한아이디, 입력한비번); db.collection('login').findOne({ id: 입력한아이디 }, function (에러, 결과) { if (에러) return done(에러)
if (!결과) return done(null, false, { message: '존재하지않는 아이디요' }) if (입력한비번 == 결과.pw) { return done(null, 결과) } else { return done(null, false, { message: '비번틀렸어요' }) } }) }));
passport.serializeUser(function (user, done) { done(null, user.id) });
passport.deserializeUser(function (아이디, done) { db.collection('login').findOne({id: 아이디}, function(에러,결과){ done(null, 결과) }) });
app.get('/write', function(요청, 응답) { 응답.render('write.ejs') });
app.post('/register', function(요청, 응답){ db.collection('login').insertOne({id: 요청.body.id, pw: 요청.body.pw}, function(에러,결과){ 응답.redirect('/') }) })
app.get('/search', (요청, 응답)=>{ console.log(요청.query); db.collection('post').find( { $text : { $search: 요청.query.value }} ).toArray((에러, 결과)=>{ console.log(결과) 응답.render('search.ejs', {posts : 결과}) }) })
app.post('/add', function (요청, 응답) { db.collection('counter').findOne({name : '게시물갯수'}, function(에러, 결과){ var 총게시물갯수 = 결과.totalPost
var 저장할거 = {_id : 총게시물갯수 + 1, 제목 : 요청.body.title, 날짜 : 요청.body.date , 작성자: 요청.user._id } // var 저장할거 = {_id : 총게시물갯수 + 1, 제목 : 요청.body.title, 날짜 : 요청.body.date} db.collection('post').insertOne(저장할거, function (에러, 결과) { console.log('저장완료'); db.collection('counter').updateOne({name:'게시물갯수'},{ $inc: {totalPost:1} },function(에러, 결과){console.log('게시물 갯수 증가 완료'); if(에러){return console.log(에러)} }) }) }) })
app.get('/shop/shirts', function(요청, 응답){ 응답.send('셔츠 파는 페이지입니다.'); }); app.get('/shop/pants', function(요청, 응답){ 응답.send('바지 파는 페이지입니다.'); });
let multer = require('multer'); var storage = multer.diskStorage({ destination : function(req, file, cb){ cb(null, './public/image') }, filename : function(req, file, cb){ cb(null, file.originalname ) } }); var upload = multer({storage : storage});
app.get('/upload', function(요청, 응답){ 응답.render('upload.ejs') });
app.post('/upload', upload.single('프로필'), function(요청,응답){ 응답.send('업로드완료') } );
//업로드한 이미지 보여주는 법 (이미지 API 만들기) app.get('/image/:imageName', function(요청, 응답){ 응답.sendFile( __dirname + '/public/image/' + 요청.params.imageName ) })
-
글쓴이글
4 글 보임 - 1 에서 4 까지 (총 4 중에서)
- 답변은 로그인 후 가능합니다.