Skip to content

Commit 408a40a

Browse files
committed
Add Resource routes
1 parent 4f49151 commit 408a40a

File tree

4 files changed

+56
-3
lines changed

4 files changed

+56
-3
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"dotenv": "^8.1.0",
3030
"express": "^4.17.1",
3131
"pg": "^7.12.1",
32-
"sequelize": "^5.18.0"
32+
"sequelize": "^5.18.0",
33+
"uuid": "^3.3.3"
3334
}
3435
}

routes/index.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import resource from "./resource";
2+
3+
export { resource };

routes/resource.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { uuidv4 as uuid } from "uuid/v4";
2+
import { Router } from "express";
3+
4+
const router = Router();
5+
6+
router.get("/", (req, res) => {
7+
return res.send(Object.values(req.context.models.resources));
8+
});
9+
10+
router.get("/:resourceId", (req, res) => {
11+
return res.send(req.context.models.resources[req.params.resourceId]);
12+
});
13+
14+
router.post("/", (req, res) => {
15+
const id = uuid();
16+
const resource = {
17+
id,
18+
title: req.body.title,
19+
description: req.body.description,
20+
url: req.body.url,
21+
published: Date.now(),
22+
created: Date.now()
23+
};
24+
25+
// TODO: Sequelize
26+
27+
return res.send(resource);
28+
});
29+
30+
export default router;

src/index.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import "dotenv/config";
22
import cors from "cors";
33
import express from "express";
4+
import routes from "./routes";
45

56
const app = express();
67

@@ -9,8 +10,26 @@ const app = express();
910
// (i.e., whitelisting domains): https://github.com/expressjs/cors
1011
app.use(cors());
1112

13+
// Allows receiving of a JSON or URL Encoded payload
14+
app.use(express.json());
15+
app.use(express.urlencoded({ extended: true }));
16+
17+
// App level function used to intercept any requests and do something before
18+
// sending a response back (i.e., defining context or authentication).
19+
app.use((req, res, next) => {
20+
// The next function is called to signalize that the middleware has finished
21+
// its job. Important for when middleware uses asynchronous functions.
22+
23+
// TODO: Define context.models.resources
24+
next();
25+
});
26+
27+
app.use("/resources", routes.resource);
28+
1229
app.get("/", (req, res) => {
13-
res.send("hello world");
30+
return res.send("Recieved a GET HTTP method");
1431
});
1532

16-
app.listen(3000, () => console.log("listening on port 3000"));
33+
app.listen(process.env.PORT, () => {
34+
console.log(`Listening on port ${process.env.PORT}`);
35+
});

0 commit comments

Comments
 (0)