Bläddra i källkod

:recycle: starting to architect backend

master
TOJ 5 år sedan
förälder
incheckning
d7b82a591d
6 ändrade filer med 2545 tillägg och 199 borttagningar
  1. 4
    0
      backend/api/index.js
  2. 19
    0
      backend/api/profile.js
  3. 2462
    199
      backend/package-lock.json
  4. 4
    0
      backend/package.json
  5. 38
    0
      backend/src/index.js
  6. 18
    0
      backend/src/routes.js

+ 4
- 0
backend/api/index.js Visa fil

@@ -0,0 +1,4 @@
1
+const profile = require('./profile')
2
+module.exports = {
3
+    profile
4
+};

+ 19
- 0
backend/api/profile.js Visa fil

@@ -0,0 +1,19 @@
1
+// https://github.com/connor11528/task-app-backend/blob/master/src/api/index.js
2
+
3
+const profileApi = {
4
+  get: {
5
+    async handler(request, h) {
6
+      try {
7
+        console.log('get handler for profile');
8
+
9
+        // You have to return SOMETHING
10
+        return { thing: 'profile' }
11
+
12
+      } catch (err) {
13
+        //   Boom.badImplementation(err);
14
+      }
15
+    }
16
+  }
17
+};
18
+
19
+module.exports = profileApi;

+ 2462
- 199
backend/package-lock.json
Filskillnaden har hållits tillbaka eftersom den är för stor
Visa fil


+ 4
- 0
backend/package.json Visa fil

@@ -4,11 +4,15 @@
4 4
   "description": "",
5 5
   "main": "index.js",
6 6
   "scripts": {
7
+    "start": "nodemon src/index.js",
7 8
     "test": "echo \"Error: no test specified\" && exit 1"
8 9
   },
9 10
   "author": "TOJ",
10 11
   "license": "UNLICENSED",
11 12
   "dependencies": {
12 13
     "@hapi/hapi": "^20.1.3"
14
+  },
15
+  "devDependencies": {
16
+    "nodemon": "^2.0.7"
13 17
   }
14 18
 }

+ 38
- 0
backend/src/index.js Visa fil

@@ -0,0 +1,38 @@
1
+'use strict';
2
+
3
+const api = require('../api')
4
+const routes = require('./routes')
5
+
6
+const Hapi = require('@hapi/hapi');
7
+
8
+const server = Hapi.server({
9
+    port: 3001,
10
+    host: 'localhost',
11
+    routes: { cors: true }
12
+});
13
+
14
+const init = async () => {
15
+    try {
16
+        routes.forEach(route => {
17
+            server.route(route);
18
+        })
19
+
20
+        await server.start();
21
+
22
+        console.log('Server running on %s', server.info.uri);
23
+    }
24
+
25
+    catch(err) {
26
+        console.error(err);
27
+    }
28
+
29
+};
30
+
31
+process.on('unhandledRejection', (err) => {
32
+    console.log(err);
33
+    process.exit(1);
34
+});
35
+
36
+init();
37
+
38
+module.exports = server;

+ 18
- 0
backend/src/routes.js Visa fil

@@ -0,0 +1,18 @@
1
+const api = require('../api')
2
+
3
+const routes = [
4
+    {
5
+        method: 'GET',
6
+        path: '/api',
7
+        handler: (request, h) => {
8
+            return { success: true }
9
+        }
10
+    },
11
+    {
12
+        method: 'GET',
13
+        path: '/api/profiles',
14
+        options: api.profile.get
15
+    },
16
+]
17
+
18
+module.exports = routes

Laddar…
Avbryt
Spara