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-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..f3616e9 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" }, @@ -16,6 +17,7 @@ "typescript": "^3.9.5" }, "scripts": { - "start": "nodemon" + "start": "nodemon src/app.ts", + "debug": "nodemon --inspect src/app.ts" } } 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/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); 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)); };