Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions gateway-express/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/
.DS_Store
24 changes: 24 additions & 0 deletions gateway-express/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Intro
This is a REST gateway written in javascript with Node.js and Express.
## Stage One
For each incoming request, it issues a HTTP request to the RestServ and return HTML document.
```
GET /rest
```
## Stage Two
For each incoming request, it issues n HTTP requests to the RestServ and return content in accept type.
```
POST /rest
```
# Quick Start
0. Set up environment. [Download Node.js](https://nodejs.org/en/download/package-manager/#macos)
1. Run `npm install` to install the dependencies, `express` (web framework for Node.js) and `request` (module to make http request).
2. Start RestServer, checkout the binary file and execute the one that is suitable for you OS.
3. Start RestGateway, either `npm start` (under gateway-express folder) or `npm --prefix gateway-express start` (under main folder).
4. Send requests using `curl`.

## Sample Request Commands

- `curl -X GET localhost:5000/rest`
- `curl -d '{"numRequests": 5}' -H "Content-Type: application/json" -X POST localhost:5000/rest`
- `curl -d '{"numRequests": 5}' -H "Content-Type: application/json" -H "Accept:application/json" -X POST localhost:5000/rest`
73 changes: 73 additions & 0 deletions gateway-express/gateway.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const express = require('express');
const request = require('request');

const app = new express();
const PORT = 5000;
const RESTSERVERPORT = 8080;
// Body parser request
app.use(express.json({ extended: false}));

// @route GET '/rest'
// @desc Stage One: issue single request to restserver
app.get('/rest', (req, res) => {
try {
const options = {
url: `http://localhost:${RESTSERVERPORT}/request`,
method: 'GET'
};
request(options, (error, response, body) => {
if(error) console.error(error);
const info = JSON.parse(body);
// Set response type and response body
res.set({'Content-Type': 'text/html'});
res.send(`I RESTed for ${info.data} time units.`);
});
} catch (err) {
res.status(500).send('Server Error');
}
});

// @route POST '/rest'
// @desc Stage Two: issue n requests to restserver, and return the content with accept content type.
app.post('/rest', (req, res) => {
// Extract parameter from request body
const numRequests = req.body.numRequests;
let responseTime = [];
let requests = [];
// Declare a promise function
function issueSingleRequest(responseTime) {
const options = {
url: `http://localhost:${RESTSERVERPORT}/request`,
method: 'GET'
};
return new Promise((resolve, reject) => {
request(options, (error, response, body) => {
if(error) reject('error');
else {
responseTime.push(JSON.parse(body).data);
resolve();
}
});
});
};
for(let i = 0; i < numRequests; i++) {
requests.push(issueSingleRequest(responseTime));
};
// Issue n requests in parallel
Promise.all(requests).then(() => {
let totalTime = responseTime.reduce((a, b)=> {return a + b;}, 0);
// Test
//console.log(responseTime);

if(req.accepts('application/json')) {
res.json({"numRequests": `${numRequests}`, "milliseconds": `${totalTime}` })
} else {
res.set({'Content-Type': 'text/html'});
res.send(`I made ${numRequests} requests and it took ${totalTime} milliseconds`);
}
}).catch((error) => res.status(500).send('Server Error'))

});

app.listen(PORT, console.log(`Gateway is listening on port: ${PORT}`));

Loading