Skip to content

Commit

Permalink
adding starter code
Browse files Browse the repository at this point in the history
  • Loading branch information
shaunganley committed Apr 11, 2024
1 parent d03a82f commit c21df61
Show file tree
Hide file tree
Showing 8 changed files with 2,821 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
dist
node_modules
.DS_Store
2,709 changes: 2,709 additions & 0 deletions package-lock.json

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "nodejs-express-axios-starter",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "tsc && node dist/app.js",
"dev": "nodemon src/app.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "Shaun Ganley <[email protected]>",
"license": "ISC",
"dependencies": {
"axios": "^1.6.8",
"body-parser": "^1.20.2",
"express": "^4.19.2",
"express-session": "^1.18.0",
"nunjucks": "^3.2.4"
},
"devDependencies": {
"@types/axios": "^0.14.0",
"@types/express": "^4.17.21",
"@types/express-session": "^1.18.0",
"@types/node": "^20.11.30",
"@types/nunjucks": "^3.2.6",
"nodemon": "^3.1.0",
"ts-node": "^10.9.2"
}
}
32 changes: 32 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import express from "express";
import nunjucks from "nunjucks";
import bodyParser from "body-parser";
import session from "express-session";

import { getAllDatabases } from "./controllers/TestController";

const app = express();

const env = nunjucks.configure('views', {
autoescape: true,
express: app
});

app.use(bodyParser.json())
app.use(bodyParser.urlencoded({
extended: true
}))

app.use(session({ secret: 'SUPER_SECRET', cookie: { maxAge: 28800000 }}));

declare module "express-session" {
interface SessionData {
token: string;
}
}

app.listen(3000, () => {
console.log('Server started on port 3000');
});

app.get('/', getAllDatabases);
6 changes: 6 additions & 0 deletions src/controllers/TestController.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import express from "express";
import { getDatabases } from "../services/TestService"

export const getAllDatabases = async (req: express.Request, res: express.Response): Promise<void> => {
res.render('databaseList.html', { databases: await getDatabases() });
}
12 changes: 12 additions & 0 deletions src/services/TestService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import axios, { AxiosResponse } from "axios";

export const getDatabases = async (): Promise<string[]> => {
try {
const response: AxiosResponse = await axios.get("http://localhost:8080/api/test");

return response.data;
} catch (e) {
console.log(e);
throw new Error('Failed to get databases');
}
}
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "./dist"
},
"include": [
"src/**/*"
]
}
14 changes: 14 additions & 0 deletions views/databaseList.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!DOCTYPE html>
<html>
<head>
<title>Database List</title>
</head>
<body>
<h1>Database List</h1>
<ul>
{% for name in databases %}
<li>{{ name }}</li>
{% endfor %}
</ul>
</body>
</html>

0 comments on commit c21df61

Please sign in to comment.