Fix /api/getRequests endpoint #1

Closed
Ghost wants to merge 3 commits from temp-jenn into temp
7 changed files with 34 additions and 8 deletions

13
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,13 @@
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "attach",
"name": "Node: Nodemon",
"processId": "${command:PickProcess}",
"restart": true,
"protocol": "inspector"
}
]
}

View File

@ -2,5 +2,9 @@
"watch": ["src"], "watch": ["src"],
"ext": "ts", "ext": "ts",
"ignore": ["src/**/*.spec.ts"], "ignore": ["src/**/*.spec.ts"],
"exec": "ts-node ./src/app.ts" "execMap": {
"ts": "node --require ts-node/register"
},
"verbose": true,
"restartable": "rs"
} }

5
package-lock.json generated
View File

@ -491,6 +491,11 @@
"is-obj": "^2.0.0" "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": { "duplexer3": {
"version": "0.1.4", "version": "0.1.4",
"resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz", "resolved": "https://registry.npmjs.org/duplexer3/-/duplexer3-0.1.4.tgz",

View File

@ -4,6 +4,7 @@
"main": "index.js", "main": "index.js",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"dotenv": "^8.2.0",
"express": "^4.17.1", "express": "^4.17.1",
"pg": "^8.2.1" "pg": "^8.2.1"
}, },
@ -16,6 +17,7 @@
"typescript": "^3.9.5" "typescript": "^3.9.5"
}, },
"scripts": { "scripts": {
"start": "nodemon" "start": "nodemon src/app.ts",
"debug": "nodemon --inspect src/app.ts"
} }
} }

View File

@ -2,6 +2,7 @@ import * as config from "./config";
import * as requests from "./requests"; import * as requests from "./requests";
import express from "express"; import express from "express";
import db from "./db"; import db from "./db";
import { QueryResult } from "pg";
const app = express(); const app = express();
app.use(express.static('public')); app.use(express.static('public'));
@ -9,8 +10,7 @@ app.use(express.json());
app.get("/api/getRequests", async (request, response) => { app.get("/api/getRequests", async (request, response) => {
var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 ); var requestCount = ( request.query.count ? parseInt(request.query.count as string, 10) : 5 );
requests.getRequests(requestCount).then((val: any) => console.log(val)); requests.getRequests(requestCount).then((val: QueryResult) => response.send(val));
response.send(requests.getRequests(requestCount));
}); });
app.get("/api/getAllRequests", async (request, response) => { app.get("/api/getAllRequests", async (request, response) => {

View File

@ -1,3 +1,5 @@
require('dotenv').config()
if (!process.env.PORT) { if (!process.env.PORT) {
console.log("Missing environment variable PORT"); console.log("Missing environment variable PORT");
process.exit(1); process.exit(1);

View File

@ -10,7 +10,7 @@ const getRequestsQuery = {
export async function getRequests(count: number) { export async function getRequests(count: number) {
var query = Object.assign(getRequestsQuery, { values: [count] }); var query = Object.assign(getRequestsQuery, { values: [count] });
db.query(query) return db.query(query)
.then((result: pg.QueryResult) => result.rows) .then((result: pg.QueryResult) => result.rows)
.catch((e: any) => console.error(e.stack)); .catch((e: any) => console.error(e.stack));
}; };