1
1
const express = require ( "express" ) ;
2
2
const parser = require ( "body-parser" ) ;
3
3
const mongoose = require ( "mongoose" ) ;
4
- const Post = require ( "./models/post" ) ;
5
4
6
- require ( 'dotenv/config' ) ;
5
+ const postsRoutes = require ( "./routes/posts" ) ;
6
+
7
+ require ( "dotenv/config" ) ;
7
8
8
9
const app = express ( ) ;
9
10
10
11
mongoose
11
- . connect (
12
- process . env . DB_CONNECTION ,
13
- { useNewUrlParser : true , useUnifiedTopology : true }
14
- )
12
+ . connect ( process . env . DB_CONNECTION , {
13
+ useNewUrlParser : true ,
14
+ useUnifiedTopology : true ,
15
+ } )
15
16
. then ( ( ) => {
16
17
console . log ( "Connected to database!" ) ;
17
18
} )
@@ -34,38 +35,6 @@ app.use((req, res, next) => {
34
35
next ( ) ;
35
36
} ) ;
36
37
37
- app . get ( "/api/posts" , ( req , res ) => {
38
- Post . find ( )
39
- . then ( ( posts ) => {
40
- res . status ( 200 ) . json ( {
41
- message : "Posts fetched successfully" ,
42
- data : posts ,
43
- } ) ;
44
- } )
45
- . catch ( ( err ) => {
46
- console . log ( err ) ;
47
- } ) ;
48
- } ) ;
49
-
50
- app . post ( "/api/posts" , ( req , res ) => {
51
- const post = new Post ( {
52
- title : req . body . title ,
53
- content : req . body . content ,
54
- } ) ;
55
- post . save ( ) ;
56
-
57
- res . status ( 201 ) . json ( {
58
- message : "Post saved successfully" ,
59
- } ) ;
60
- } ) ;
61
-
62
- app . delete ( "/api/posts/:id" , ( req , res ) => {
63
- Post . deleteOne ( { _id : req . params . id } ) . then ( ( ) => {
64
- res . status ( 200 ) . json ( { message : "Post deleted succesffully" } ) ;
65
- } ) . catch ( ( err ) => {
66
- console . log ( err )
67
- } ) ;
68
-
69
- } ) ;
38
+ app . use ( '/api/posts' , postsRoutes ) ;
70
39
71
40
module . exports = app ;
0 commit comments