Skip to content

Commit 31dc811

Browse files
first commit
0 parents  commit 31dc811

File tree

16 files changed

+9560
-0
lines changed

16 files changed

+9560
-0
lines changed

Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: node app.js

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
ReqRes
2+
======
3+
4+
ReqRes is a bare-bones ExpressJS application.
5+
6+
[Docs & Demos ⇒](http://reqres.in)
7+
8+
## Installation
9+
10+
* Clone repo
11+
* Make sure Node.js is installed on your machine
12+
* `npm install`
13+
* Make sure you have Gulp installed globally (only needed for asset compilation)
14+
* `node app.js` or use [Nodemon](https://github.com/remy/nodemon)
15+
* Run `gulp` if you're modifying the Sass
16+
17+
## Tour
18+
19+
* [app.js](https://github.com/benhowdle89/reqres/blob/master/app.js) - this is where we create the Express app and define all of our routes
20+
* [index.js](https://github.com/benhowdle89/reqres/blob/master/routes/index.js) - this is the main file for the callback routes
21+
* [config.json](https://github.com/benhowdle89/reqres/blob/master/config.json) - this houses the pagination details & fake session token
22+
* [data.json](https://github.com/benhowdle89/reqres/blob/master/data.json) - this holds the fake data. This is where you would add a new key to the array and then you could define a callback route for it in `routes/index.js`

app.js

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
var express = require("express"),
2+
bodyParser = require("body-parser"),
3+
hbs = require("hbs"),
4+
path = require("path"),
5+
cors = require("cors"),
6+
app = express(),
7+
port = process.env.PORT || 5000;
8+
9+
var getRandomInteger = function(min, max) {
10+
min = Math.ceil(min);
11+
max = Math.floor(max);
12+
return Math.floor(Math.random() * (max - min)) + min;
13+
}
14+
15+
app.use(bodyParser.urlencoded({
16+
extended: false
17+
}));
18+
19+
app.use(bodyParser.json());
20+
21+
app.use(cors());
22+
23+
app.set("views", __dirname + "/views");
24+
app.set("view engine", "html");
25+
app.set("view options", { layout: "layout.html" });
26+
app.engine("html", hbs.__express);
27+
app.use(express.static(path.join(__dirname, "public")));
28+
29+
var routes = require("./routes/");
30+
31+
app.all("/api/*", [
32+
function(req, res, next) {
33+
if (req.query && req.query.delay) {
34+
var delay = req.query.delay;
35+
if(delay === 'random'){
36+
var random = getRandomInteger(200, 3000);
37+
return setTimeout(next, random);
38+
}
39+
if(isNaN(delay)){
40+
return next();
41+
}
42+
return setTimeout(next, req.query.delay * 1000);
43+
}
44+
return next();
45+
}
46+
]);
47+
48+
app.get("/", function(req, res, next) {
49+
res.render("index");
50+
});
51+
52+
app.post("/api/login", routes.login);
53+
app.post("/api/login/", routes.login);
54+
55+
app.post("/api/register", routes.register);
56+
app.post("/api/register/", routes.register);
57+
58+
app.post("/api/logout", routes.logout);
59+
app.post("/api/logout/", routes.logout);
60+
61+
app.get("/api/:resource/*", routes.get);
62+
app.get("/api/:resource", routes.get);
63+
64+
app.post("/api/:resource/*", routes.post);
65+
app.post("/api/:resource", routes.post);
66+
67+
app.put("/api/:resource/*", routes.put);
68+
app.put("/api/:resource", routes.put);
69+
70+
app.patch("/api/:resource/*", routes.patch);
71+
app.patch("/api/:resource", routes.patch);
72+
73+
app.delete("/api/:resource/*", routes.delete);
74+
app.delete("/api/:resource", routes.delete);
75+
76+
app.use(function(req, res, next) {
77+
res.status(404);
78+
79+
if (req.accepts("html")) {
80+
res.render("404", {
81+
url: req.url
82+
});
83+
return;
84+
}
85+
86+
res.type("txt").send("Not found");
87+
});
88+
89+
var server = app.listen(port, function() {
90+
91+
var host = server.address().address,
92+
port = server.address().port;
93+
94+
console.log("reqres app listening at http://%s:%s", host, port);
95+
96+
});

0 commit comments

Comments
 (0)