File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -18,7 +18,7 @@ const dbConfig = config.get('DBHost');
1818
1919
2020
21- const index = require ( './routes/index ' ) ;
21+ const course = require ( './routes/course ' ) ;
2222const user = require ( './routes/user' ) ;
2323const app = express ( ) ;
2424
@@ -75,7 +75,7 @@ app.use(bodyParser.urlencoded({ extended: false }));
7575app . use ( cookieParser ( ) ) ;
7676app . use ( express . static ( path . join ( __dirname , 'public' ) ) ) ;
7777
78- app . use ( '/' , index ) ;
78+ app . use ( '/api/courses ' , course ) ;
7979app . use ( '/api/users' , user ) ;
8080
8181// catch 404 and forward to error handler
Original file line number Diff line number Diff line change 1+ const express = require ( 'express' ) ;
2+ const router = express . Router ( ) ;
3+ const Course = require ( '../models/course' ) ;
4+
5+ router . get ( '/' , function ( req , res , next ) {
6+ res . send ( "hello!" ) ;
7+ } ) ;
8+
9+
10+ router . post ( '/' , ( req , res , next ) => {
11+ if ( ! req . body . title || ! req . body . description ) {
12+ const err = new Error ( "Bad Request" ) ;
13+ err . status = 400 ;
14+ return next ( err ) ;
15+ }
16+ const courseData = req . body ;
17+ Course . create ( courseData , ( err , course ) => {
18+ if ( err ) {
19+ return next ( err ) ;
20+ }
21+ return res . status ( 201 ) . json ( { message : "User Successfully added!" , status : 201 , course} ) ;
22+ } ) ;
23+ } ) ;
24+
25+
26+ module . exports = router ;
Load diff This file was deleted.
Original file line number Diff line number Diff line change @@ -11,6 +11,8 @@ const should = chai.should();
1111
1212chai . use ( chaiHttp ) ;
1313
14+ let courseIndexLink = '/api/courses/' ;
15+
1416describe ( 'Courses' , ( ) => {
1517 beforeEach ( ( done ) => {
1618 Course . remove ( { } , ( err ) => {
@@ -25,7 +27,7 @@ describe('Courses', () => {
2527 describe ( '/GET course' , ( ) => {
2628 it ( 'should GET all the courses' , ( done ) => {
2729 chai . request ( server )
28- . get ( '/course' )
30+ . get ( courseIndexLink )
2931 . end ( ( err , res ) => {
3032 res . should . have . status ( 200 ) ;
3133 done ( ) ;
@@ -50,7 +52,7 @@ describe('Courses', () => {
5052 ]
5153 } ;
5254 chai . request ( server )
53- . post ( '/course' )
55+ . post ( courseIndexLink )
5456 . send ( course )
5557 . end ( ( err , res ) => {
5658 res . should . have . status ( 201 ) ;
@@ -99,7 +101,7 @@ describe('Courses', () => {
99101 } ) ;
100102 function postCourse ( course , done ) {
101103 chai . request ( server )
102- . post ( '/course' )
104+ . post ( courseIndexLink )
103105 . send ( course )
104106 . end ( ( err , res ) => {
105107 res . should . have . status ( 400 ) ;
You can’t perform that action at this time.
0 commit comments