Skip to content

Commit 8b12e61

Browse files
committed
Express Generator REST API
1 parent 5446078 commit 8b12e61

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

Server-side Development with NodeJS, Express and MongoDB/conFusionServer/app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ var logger = require('morgan');
66

77
var indexRouter = require('./routes/index');
88
var usersRouter = require('./routes/users');
9+
var dishRouter = require('./routes/dishRouter');
10+
var promoRouter = require('./routes/promoRouter');
11+
var leaderRouter = require('./routes/leaderRouter');
912

1013
var app = express();
1114

@@ -21,6 +24,9 @@ app.use(express.static(path.join(__dirname, 'public')));
2124

2225
app.use('/', indexRouter);
2326
app.use('/users', usersRouter);
27+
app.use('/dishes',dishRouter);
28+
app.use('/promotions',promoRouter);
29+
app.use('/leaders',leaderRouter);
2430

2531
// catch 404 and forward to error handler
2632
app.use(function(req, res, next) {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<title>This is about Us </title>
3+
<body>
4+
<h1>About Us.html</h1>
5+
<p>This is the contents of this file</p>
6+
</body>
7+
</html>
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<html>
2+
<title>This is index.html</title>
3+
<body>
4+
<h1>Index.html</h1>
5+
<p>This is the contents of this file</p>
6+
</body>
7+
</html>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
const express = require('express');
2+
const bodyParser = require('body-parser');
3+
4+
const dishRouter = express.Router();
5+
6+
dishRouter.use(bodyParser.json());
7+
8+
dishRouter.route('/')
9+
.all((req,res,next) => {
10+
res.statusCode = 200;
11+
res.setHeader('Content-Type', 'text/plain');
12+
next();
13+
})
14+
.get((req,res,next) => {
15+
res.end('Will send all the dishes to you!');
16+
})
17+
.post((req, res, next) => {
18+
res.end('Will add the dish: ' + req.body.name + ' with details: ' + req.body.description);
19+
})
20+
.put((req, res, next) => {
21+
res.statusCode = 403;
22+
res.end('PUT operation not supported on /dishes');
23+
})
24+
.delete((req, res, next) => {
25+
res.end('Deleting all dishes');
26+
});
27+
28+
29+
30+
31+
32+
dishRouter.route('/:dishId')
33+
.all((req,res,next) => {
34+
res.statusCode = 200;
35+
res.setHeader('Content-Type', 'text/plain');
36+
next();
37+
})
38+
.get((req,res,next) => {
39+
res.end('Will send details of the dish: ' + req.params.dishId +' to you!');
40+
})
41+
.post((req, res, next) => {
42+
res.statusCode = 403;
43+
res.end('POST operation not supported on /dishes/' + req.params.dishId);
44+
})
45+
.put((req, res, next) => {
46+
47+
res.write('Updating the dish: ' + req.params.dishId +"\n");
48+
res.end('Will update the dish: ' + req.body.name + ' with details: ' + req.body.description);
49+
})
50+
.delete((req, res, next) => {
51+
res.end('Deleting dish: ' + req.params.dishId);
52+
});
53+
54+
55+
56+
57+
58+
59+
60+
61+
module.exports = dishRouter;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const express = require('express');
2+
const bodyParser = require('body-parser');
3+
4+
const leaderRouter = express.Router();
5+
leaderRouter.use(bodyParser.json());
6+
7+
leaderRouter.route('/')
8+
.all((req,res,next) => {
9+
res.statusCode = 200;
10+
res.setHeader('Content-Type', 'text/plain');
11+
next();
12+
})
13+
.get((req,res,next) => {
14+
res.end('Will send all the leaders to you!');
15+
})
16+
.post((req, res, next) => {
17+
res.end('Will add the leader: ' + req.body.name + ' with details: ' + req.body.description);
18+
})
19+
.put((req, res, next) => {
20+
res.statusCode = 403;
21+
res.end('PUT operation not supported on /leaders');
22+
})
23+
.delete((req, res, next) => {
24+
res.end('Deleting all leaders');
25+
});
26+
27+
leaderRouter.route('/:leaderId')
28+
.all((req,res,next) => {
29+
res.statusCode = 200;
30+
res.setHeader('Content-Type', 'text/plain');
31+
next();
32+
})
33+
.get((req,res,next) => {
34+
res.end('Will send details of the leader: ' + req.params.leaderId +' to you!');
35+
})
36+
.post((req, res, next) => {
37+
res.statusCode = 403;
38+
res.end('POST operation not supported on /leaders/' + req.params.leaderId);
39+
})
40+
.put((req, res, next) => {
41+
42+
res.write('Updating the leader: ' + req.params.leaderId +"\n");
43+
res.end('Will update the leader: ' + req.body.name + ' with details: ' + req.body.description);
44+
})
45+
.delete((req, res, next) => {
46+
res.end('Deleting leader: ' + req.params.leaderId);
47+
});
48+
49+
module.exports = leaderRouter;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const express = require('express');
2+
const bodyParser = require('body-parser');
3+
4+
const promoRouter = express.Router();
5+
promoRouter.use(bodyParser.json());
6+
7+
promoRouter.route('/')
8+
.all((req,res,next) => {
9+
res.statusCode = 200;
10+
res.setHeader('Content-Type', 'text/plain');
11+
next();
12+
})
13+
.get((req,res,next) => {
14+
res.end('Will send all the promotions to you!');
15+
})
16+
.post((req, res, next) => {
17+
res.end('Will add the promotion: ' + req.body.name + ' with details: ' + req.body.description);
18+
})
19+
.put((req, res, next) => {
20+
res.statusCode = 403;
21+
res.end('PUT operation not supported on /promotions');
22+
})
23+
.delete((req, res, next) => {
24+
res.end('Deleting all promotions');
25+
});
26+
27+
promoRouter.route('/:promoId')
28+
.all((req,res,next) => {
29+
res.statusCode = 200;
30+
res.setHeader('Content-Type', 'text/plain');
31+
next();
32+
})
33+
.get((req,res,next) => {
34+
res.end('Will send details of the promotion: ' + req.params.promoId +' to you!');
35+
})
36+
.post((req, res, next) => {
37+
res.statusCode = 403;
38+
res.end('POST operation not supported on /promotions/' + req.params.promoId);
39+
})
40+
.put((req, res, next) => {
41+
42+
res.write('Updating the promotion: ' + req.params.promoId +"\n");
43+
res.end('Will update the promotion: ' + req.body.name + ' with details: ' + req.body.description);
44+
})
45+
.delete((req, res, next) => {
46+
res.end('Deleting promotion: ' + req.params.promoId);
47+
});
48+
49+
module.exports = promoRouter;

0 commit comments

Comments
 (0)