File tree Expand file tree Collapse file tree 4 files changed +56
-3
lines changed Expand file tree Collapse file tree 4 files changed +56
-3
lines changed Original file line number Diff line number Diff line change 29
29
"dotenv" : " ^8.1.0" ,
30
30
"express" : " ^4.17.1" ,
31
31
"pg" : " ^7.12.1" ,
32
- "sequelize" : " ^5.18.0"
32
+ "sequelize" : " ^5.18.0" ,
33
+ "uuid" : " ^3.3.3"
33
34
}
34
35
}
Original file line number Diff line number Diff line change
1
+ import resource from "./resource" ;
2
+
3
+ export { resource } ;
Original file line number Diff line number Diff line change
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 ;
Original file line number Diff line number Diff line change 1
1
import "dotenv/config" ;
2
2
import cors from "cors" ;
3
3
import express from "express" ;
4
+ import routes from "./routes" ;
4
5
5
6
const app = express ( ) ;
6
7
@@ -9,8 +10,26 @@ const app = express();
9
10
// (i.e., whitelisting domains): https://github.com/expressjs/cors
10
11
app . use ( cors ( ) ) ;
11
12
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
+
12
29
app . get ( "/" , ( req , res ) => {
13
- res . send ( "hello world " ) ;
30
+ return res . send ( "Recieved a GET HTTP method " ) ;
14
31
} ) ;
15
32
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
+ } ) ;
You can’t perform that action at this time.
0 commit comments