Skip to content

Commit 92afb2e

Browse files
update router & tests
1 parent 75b5a49 commit 92afb2e

4 files changed

Lines changed: 33 additions & 36 deletions

File tree

src/app.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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');
2222
const user = require('./routes/user');
2323
const app = express();
2424

@@ -75,7 +75,7 @@ app.use(bodyParser.urlencoded({ extended: false }));
7575
app.use(cookieParser());
7676
app.use(express.static(path.join(__dirname, 'public')));
7777

78-
app.use('/', index);
78+
app.use('/api/courses', course);
7979
app.use('/api/users', user);
8080

8181
// catch 404 and forward to error handler

src/routes/course.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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;

src/routes/index.js

Lines changed: 0 additions & 31 deletions
This file was deleted.

test/course_test.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const should = chai.should();
1111

1212
chai.use(chaiHttp);
1313

14+
let courseIndexLink = '/api/courses/';
15+
1416
describe('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);

0 commit comments

Comments
 (0)