File tree 6 files changed +92
-0
lines changed
6 files changed +92
-0
lines changed Original file line number Diff line number Diff line change
1
+ /node_modules
2
+ .DS_Store
3
+ package-lock.json
Original file line number Diff line number Diff line change
1
+ const express = require ( 'express' ) ;
2
+ const bodyParser = require ( 'body-parser' ) ;
3
+ const app = express ( ) ;
4
+
5
+ const Note = require ( './db/models/note.js' ) . Note ;
6
+
7
+ app . use ( bodyParser . json ( ) ) ;
8
+
9
+ app . get ( '/notes' , ( req , res ) => {
10
+ Note . find ( )
11
+ . then ( ( notes ) => res . status ( 200 ) . send ( notes ) )
12
+ . catch ( ( err ) => res . status ( 400 ) . send ( err ) ) ;
13
+ } ) ;
14
+
15
+ app . post ( '/notes' , ( req , res ) => {
16
+ const body = req . body ;
17
+ const note = new Note ( {
18
+ name : body . name ,
19
+ text : body . text
20
+ } ) ;
21
+ note . save ( note )
22
+ . then ( ( note ) => res . status ( 201 ) . send ( note ) )
23
+ . catch ( ( err ) => res . status ( 400 ) . send ( err ) ) ;
24
+ } ) ;
25
+
26
+ module . exports = app ;
Original file line number Diff line number Diff line change
1
+ const mongoose = require ( 'mongoose' ) ;
2
+ const DB_URI = 'mongodb://localhost:27017/myapp' ;
3
+
4
+ function connect ( ) {
5
+ return new Promise ( ( resolve , reject ) => {
6
+ mongoose . connect ( DB_URI ,
7
+ { useNewUrlParser : true , useCreateIndex : true } )
8
+ . then ( ( res , err ) => {
9
+ if ( err ) return reject ( err ) ;
10
+ resolve ( ) ;
11
+ } )
12
+ } ) ;
13
+ }
14
+
15
+ function close ( ) {
16
+ return mongoose . disconnect ( ) ;
17
+ }
18
+
19
+ module . exports = { connect, close } ;
Original file line number Diff line number Diff line change
1
+ const mongoose = require ( 'mongoose' ) ;
2
+
3
+ const NoteSchema = new mongoose . Schema ( {
4
+ name : {
5
+ type : String ,
6
+ required : true ,
7
+ unique : true ,
8
+ } ,
9
+ text : {
10
+ type : String ,
11
+ required : true ,
12
+ } ,
13
+ } ) ;
14
+
15
+ const Note = mongoose . model ( 'Note' , NoteSchema )
16
+
17
+ module . exports = { Note } ;
Original file line number Diff line number Diff line change
1
+ {
2
+ "name" : " express-mongo" ,
3
+ "version" : " 1.0.0" ,
4
+ "description" : " " ,
5
+ "main" : " index.js" ,
6
+ "scripts" : {
7
+ "start" : " nodemon server.js"
8
+ },
9
+ "author" : " " ,
10
+ "license" : " ISC" ,
11
+ "dependencies" : {
12
+ "body-parser" : " ^1.18.3" ,
13
+ "express" : " ^4.16.4" ,
14
+ "mongoose" : " ^5.4.1"
15
+ }
16
+ }
Original file line number Diff line number Diff line change
1
+ const app = require ( './app.js' ) ;
2
+ const db = require ( './db/index.js' ) ;
3
+
4
+ const PORT = process . env . PORT || 5000 ;
5
+
6
+ db . connect ( )
7
+ . then ( ( ) => {
8
+ app . listen ( PORT , ( ) => {
9
+ console . log ( 'Listening on port: ' + PORT ) ;
10
+ } ) ;
11
+ } ) ;
You can’t perform that action at this time.
0 commit comments