임태민

Update project directory

- Update backend files
...@@ -4,6 +4,7 @@ const bodyParser = require('body-parser'); ...@@ -4,6 +4,7 @@ const bodyParser = require('body-parser');
4 const methodOverride = require('body-parser'); 4 const methodOverride = require('body-parser');
5 const app = express(); 5 const app = express();
6 6
7 +
7 // DB Setting 8 // DB Setting
8 mongoose.set('useNewUrlParser', true); 9 mongoose.set('useNewUrlParser', true);
9 mongoose.set('useFindAndModify', false); 10 mongoose.set('useFindAndModify', false);
...@@ -16,16 +17,18 @@ mongoose.connect('mongodb+srv://Mapmory_admin:admin@cluster0.ncnjj.mongodb.net/P ...@@ -16,16 +17,18 @@ mongoose.connect('mongodb+srv://Mapmory_admin:admin@cluster0.ncnjj.mongodb.net/P
16 // Store DB in the variable 'db' 17 // Store DB in the variable 'db'
17 var db = mongoose.connection; 18 var db = mongoose.connection;
18 19
20 +
19 // If DB is opened successfully 21 // If DB is opened successfully
20 db.once('open', function(){ 22 db.once('open', function(){
21 console.log('DB connected'); 23 console.log('DB connected');
22 -});
23 24
25 +});
24 // if DB is failed to open 26 // if DB is failed to open
25 db.on('error', function(err){ 27 db.on('error', function(err){
26 console.log('DB ERROR : ', err); 28 console.log('DB ERROR : ', err);
27 }); 29 });
28 30
31 +
29 // Settings 32 // Settings
30 app.set('view engine', 'ejs'); 33 app.set('view engine', 'ejs');
31 app.use(express.static(__dirname + '/public')); 34 app.use(express.static(__dirname + '/public'));
...@@ -34,9 +37,13 @@ app.use(bodyParser.json()); ...@@ -34,9 +37,13 @@ app.use(bodyParser.json());
34 app.use(bodyParser.urlencoded({extended:true})); 37 app.use(bodyParser.urlencoded({extended:true}));
35 app.use(methodOverride('_method')); 38 app.use(methodOverride('_method'));
36 39
40 +
37 // Routes 41 // Routes
38 app.use('/', require('./routes/home')); 42 app.use('/', require('./routes/home'));
43 +app.use('/posts', require('./routes/posts'));
44 +
39 45
46 +// Server
40 var port = 3000; 47 var port = 3000;
41 app.listen(port, function(){ 48 app.listen(port, function(){
42 console.log('server on! http://localhost:' + port); 49 console.log('server on! http://localhost:' + port);
......
1 +const mongoose = require('mongoose');
2 +
3 +
4 +// Declare the schemea of post
5 +var postSchema = mongoose.Schema({
6 + title:{type:String, required:true},
7 + body:{type:String, required:true},
8 + createdAt:{type:Date, default:Date.now},
9 + updatedAt:{type:Date},
10 +});
11 +
12 +
13 +// Declare the variable
14 +var Post = mongoose.model('post', postSchema);
15 +
16 +
17 +// Export module
18 +module.exports = Post;
...\ No newline at end of file ...\ No newline at end of file
1 +const express = require('express');
2 +const router = express.Router();
3 +const Post = require('../models/Post');
4 +
5 +
6 +// Post home
7 +router.get('/', function(req, res){
8 + Post.find({})
9 + .sort('-createdAt')
10 + .exec(function(err, posts){
11 + if(err){return res.json(err)};
12 + res.render('posts/index', {posts:posts});
13 + });
14 +});
15 +
16 +
17 +// Post new
18 +router.get('/new', function(req, res){
19 + res.render('posts/new');
20 +})
21 +
22 +
23 +// Post create
24 +router.post('/', function(req, res){
25 + Post.create(req.body, function(err, post){
26 + if(err){return res.json(err)};
27 + res.redirect('/posts');
28 + });
29 +});
30 +
31 +
32 +// Post show
33 +router.get('/:id', function(req, res){
34 + Post.findOne({_id:req.params.id}, function(err, post){
35 + if(err){return res.json(err)};
36 + res.render('posts/show', {post:post});
37 + });
38 +});
39 +
40 +
41 +// Post edit
42 +router.get('/:id/edit', function(req, res){
43 + Post.findOne({_id:req.params.id}, function(err, post){
44 + if(err){return res.join(err)};
45 + res.render('posts/edit', {posts:post});
46 + });
47 +});
48 +
49 +
50 +// Post update
51 +router.get('/:id', function(req, res){
52 + req.body.updatedAt = Date.now();
53 + Post.findOneAndUpdate({_id:req.params.id}, req.body, function(err, post){
54 + if(err){return res.join(err)};
55 + res.redirect('posts/' + req.params.id);
56 + });
57 +});
58 +
59 +
60 +// Post delete
61 +router.delete('/:id', function(req, res){
62 + Post.deleteOne({_id:req.params.id}, function(err){
63 + if(err){return res.join(err)};
64 + res.redirect('/posts');
65 + });
66 +});
67 +
68 +
69 +// Export module
70 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file