From 3015eeb2626c77df780eeae73b7b32049c8e209b Mon Sep 17 00:00:00 2001 From: Jennifer Nguyen Date: Wed, 1 Jul 2020 23:56:18 -0700 Subject: [PATCH 1/3] Adding dependency to load variables from .env file --- package-lock.json | 5 +++++ package.json | 1 + src/config.ts | 6 ++++-- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index d457db1..5afbfd3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -491,6 +491,11 @@ "is-obj": "^2.0.0" } }, + "dotenv": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-8.2.0.tgz", + "integrity": "sha512-8sJ78ElpbDJBHNeBzUbUVLsqKdccaa/BXF1uPTw3GrvQTBgrQrtObr2mUrE38vzYd8cEv+m/JBfDLioYcfXoaw==" + }, "duplexer3": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", diff --git a/package.json b/package.json index 63ff166..a3aed5e 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,7 @@ "main": "index.js", "license": "MIT", "dependencies": { + "dotenv": "^8.2.0", "express": "^4.17.1", "pg": "^8.2.1" }, diff --git a/src/config.ts b/src/config.ts index d9516d7..c6344bc 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,6 +1,8 @@ +require('dotenv').config() + if (!process.env.PORT) { - console.log("Missing environment variable PORT"); - process.exit(1); + console.log("Missing environment variable PORT"); + process.exit(1); } export const port: number = parseInt(process.env.PORT as string, 10); -- 2.40.1 From 312f1324ca1a7f58270b268db68b03ef797ef1d4 Mon Sep 17 00:00:00 2001 From: Jennifer Nguyen Date: Thu, 2 Jul 2020 00:42:31 -0700 Subject: [PATCH 2/3] Setting up Node.js debugger with Nodemon and Typescript in VSCode editor Refer to [article](https://dev.to/oieduardorabelo/nodejs-with-typescript-debug-inside-vscode-and-nodemon-23o7) for more information --- .vscode/launch.json | 13 +++++++++++++ nodemon.json | 6 +++++- package.json | 3 ++- 3 files changed, 20 insertions(+), 2 deletions(-) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..747f90b --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,13 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "attach", + "name": "Node: Nodemon", + "processId": "${command:PickProcess}", + "restart": true, + "protocol": "inspector" + } + ] + } \ No newline at end of file diff --git a/nodemon.json b/nodemon.json index 00b54fa..0ca8c43 100644 --- a/nodemon.json +++ b/nodemon.json @@ -2,5 +2,9 @@ "watch": ["src"], "ext": "ts", "ignore": ["src/**/*.spec.ts"], - "exec": "ts-node ./src/app.ts" + "execMap": { + "ts": "node --require ts-node/register" + }, + "verbose": true, + "restartable": "rs" } diff --git a/package.json b/package.json index a3aed5e..f3616e9 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "typescript": "^3.9.5" }, "scripts": { - "start": "nodemon" + "start": "nodemon src/app.ts", + "debug": "nodemon --inspect src/app.ts" } } -- 2.40.1 From 032b7b02f0e949e368dd5537be692bb55a0ad446 Mon Sep 17 00:00:00 2001 From: Jennifer Nguyen Date: Thu, 2 Jul 2020 01:26:30 -0700 Subject: [PATCH 3/3] Fixed /api/getRequests endpoint to return response with result from database query --- src/app.ts | 6 +++--- src/requests.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/app.ts b/src/app.ts index f9bfa2a..ef6f8a8 100644 --- a/src/app.ts +++ b/src/app.ts @@ -2,15 +2,15 @@ import * as config from "./config"; import * as requests from "./requests"; import express from "express"; import db from "./db"; +import { QueryResult } from "pg"; const app = express(); app.use(express.static('public')); app.use(express.json()); app.get("/api/getRequests", async (request, response) => { - var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 ); - requests.getRequests(requestCount).then((val: any) => console.log(val)); - response.send(requests.getRequests(requestCount)); + var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 ); + requests.getRequests(requestCount).then((val: QueryResult) => response.send(val)); }); app.get("/api/getAllRequests", async (request, response) => { diff --git a/src/requests.ts b/src/requests.ts index a056da1..d0a7272 100644 --- a/src/requests.ts +++ b/src/requests.ts @@ -10,7 +10,7 @@ const getRequestsQuery = { export async function getRequests(count: number) { var query = Object.assign(getRequestsQuery, { values: [count] }); - db.query(query) + return db.query(query) .then((result: pg.QueryResult) => result.rows) .catch((e: any) => console.error(e.stack)); }; -- 2.40.1