Skip to content

Commit 2410e87

Browse files
committed
foundational files
1 parent 581cb5f commit 2410e87

27 files changed

Lines changed: 8644 additions & 0 deletions

.eslintignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
build
2+
dist
3+
node_modules
4+
.eslintrc.json

.eslintrc.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"env": {
3+
"node": true,
4+
"commonjs": true,
5+
"es2021": true,
6+
"jest": true
7+
},
8+
"extends": [
9+
"eslint:recommended",
10+
"plugin:import/recommended",
11+
"plugin:n/recommended",
12+
"plugin:security/recommended",
13+
"plugin:jest/recommended",
14+
"eslint-config-prettier"
15+
],
16+
"overrides": [],
17+
"parserOptions": {
18+
"ecmaVersion": "latest"
19+
},
20+
"plugins": ["import", "n", "security", "jest"],
21+
"rules": {
22+
"linebreak-style": ["error", "unix"],
23+
"eqeqeq": "error",
24+
"no-trailing-spaces": "error",
25+
"object-curly-spacing": ["error", "never"],
26+
"arrow-spacing": ["error", {"before": true, "after": true}],
27+
"no-console": 0
28+
}
29+
}

.gitignore

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,187 @@
1+
############################################# MacOS #############################################
2+
# General
3+
.DS_Store
4+
.AppleDouble
5+
.LSOverride
6+
7+
# Icon must end with two \r
8+
Icon
9+
10+
# Thumbnails
11+
._*
12+
13+
# Files that might appear in the root of a volume
14+
.DocumentRevisions-V100
15+
.fseventsd
16+
.Spotlight-V100
17+
.TemporaryItems
18+
.Trashes
19+
.VolumeIcon.icns
20+
.com.apple.timemachine.donotpresent
21+
22+
# Directories potentially created on remote AFP share
23+
.AppleDB
24+
.AppleDesktop
25+
Network Trash Folder
26+
Temporary Items
27+
.apdisk
28+
29+
*.local
30+
31+
############################################# Visual Studio Code #############################################
32+
.vscode/*
33+
# !.vscode/settings.json
34+
# !.vscode/tasks.json
35+
# !.vscode/launch.json
36+
# !.vscode/extensions.json
37+
# !.vscode/*.code-snippets
38+
39+
# Stores VSCode versions used for testing VSCode extensions
40+
.vscode-test
41+
42+
# Local History for Visual Studio Code
43+
.history/
44+
45+
# Built Visual Studio Code Extensions
46+
*.vsix
47+
48+
############################################# Node #############################################
49+
# Logs
50+
logs
51+
*.log
52+
npm-debug.log*
53+
yarn-debug.log*
54+
yarn-error.log*
55+
lerna-debug.log*
56+
.pnpm-debug.log*
57+
58+
# Diagnostic reports (https://nodejs.org/api/report.html)
59+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json
60+
61+
# Runtime data
62+
pids
63+
*.pid
64+
*.seed
65+
*.pid.lock
66+
67+
# Directory for instrumented libs generated by jscoverage/JSCover
68+
lib-cov
69+
70+
# Coverage directory used by tools like istanbul
71+
coverage
72+
*.lcov
73+
74+
# nyc test coverage
75+
.nyc_output
76+
77+
# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
78+
.grunt
79+
80+
# Bower dependency directory (https://bower.io/)
81+
bower_components
82+
83+
# node-waf configuration
84+
.lock-wscript
85+
86+
# Compiled binary addons (https://nodejs.org/api/addons.html)
87+
build/Release
88+
89+
# Dependency directories
90+
node_modules/
91+
jspm_packages/
92+
93+
# Snowpack dependency directory (https://snowpack.dev/)
94+
web_modules/
95+
96+
# TypeScript cache
97+
*.tsbuildinfo
98+
99+
# Optional npm cache directory
100+
.npm
101+
102+
# Optional eslint cache
103+
.eslintcache
104+
105+
# Optional stylelint cache
106+
.stylelintcache
107+
108+
# Microbundle cache
109+
.rpt2_cache/
110+
.rts2_cache_cjs/
111+
.rts2_cache_es/
112+
.rts2_cache_umd/
113+
114+
# Optional REPL history
115+
.node_repl_history
116+
117+
# Output of 'npm pack'
118+
*.tgz
119+
120+
# yarn v2
121+
.yarn/cache
122+
.yarn/unplugged
123+
.yarn/build-state.yml
124+
.yarn/install-state.gz
125+
.pnp.*
126+
127+
# Yarn Integrity file
128+
.yarn-integrity
129+
130+
# dotenv environment variable files
131+
.env
132+
.env.development.local
133+
.env.test.local
134+
.env.production.local
135+
.env.local
136+
137+
# parcel-bundler cache (https://parceljs.org/)
138+
.cache
139+
.parcel-cache
140+
141+
# Next.js build output
142+
.next
143+
out
144+
145+
# Nuxt.js build / generate output
146+
.nuxt
147+
148+
## dist & build
149+
# dist
150+
# build
151+
dist-ssr
152+
153+
# Gatsby files
154+
.cache/
155+
# Comment in the public line in if your project uses Gatsby and not Next.js
156+
# https://nextjs.org/blog/next-9-1#public-directory-support
157+
# public
158+
159+
# vuepress build output
160+
.vuepress/dist
161+
162+
# vuepress v2.x temp and cache directory
163+
.temp
164+
.cache
165+
166+
# Docusaurus cache and generated files
167+
.docusaurus
168+
169+
# Serverless directories
170+
.serverless/
171+
172+
# FuseBox cache
173+
.fusebox/
174+
175+
# DynamoDB Local files
176+
.dynamodb/
177+
178+
# TernJS port file
179+
.tern-port
180+
181+
############################################# other #############################################
182+
183+
certs
184+
certs/
185+
certs/*
186+
187+
sql/

.prettierrc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"arrowParens": "always",
3+
"bracketSameLine": true,
4+
"bracketSpacing": false,
5+
"embeddedLanguageFormatting": "auto",
6+
"endOfLine": "lf",
7+
"htmlWhitespaceSensitivity": "strict",
8+
"insertPragma": false,
9+
"jsxBracketSameLine": true,
10+
"jsxSingleQuote": false,
11+
"printWidth": 100,
12+
"proseWrap": "preserve",
13+
"quoteProps": "as-needed",
14+
"requirePragma": false,
15+
"semi": true,
16+
"singleQuote": false,
17+
"tabWidth": 2,
18+
"trailingComma": "es5",
19+
"useTabs": false
20+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# express-pgsql
2+
23
An Express server connected to a PostgreSQL database

__tests__/spec.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe("My Test Suite", () => {
2+
it("My Test Case", () => {
3+
expect(true).toEqual(true);
4+
});
5+
});

app.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const express = require("express");
2+
const favicon = require("serve-favicon");
3+
const helmet = require("helmet");
4+
const cors = require("cors");
5+
const loginRouter = require("./controllers/login");
6+
const logoutRouter = require("./controllers/logout");
7+
const usersRouter = require("./controllers/users");
8+
const postsRouter = require("./controllers/posts");
9+
const authorsRouter = require("./controllers/authors");
10+
const globalErrorHandler = require("./errors/globalErrorHandler");
11+
const logger = require("./logger/logger");
12+
13+
const app = express();
14+
15+
app.set("trust proxy", 1);
16+
17+
app.use(express.json());
18+
app.use(helmet());
19+
app.use(cors());
20+
// app.use(favicon(path.join(__dirname, "public", "favicon.ico")));
21+
22+
app.get("/", (req, res) => {
23+
res.json("Home");
24+
});
25+
26+
app.use("/api/login", loginRouter);
27+
app.use("/api/logout", logoutRouter);
28+
app.use("/api/users", usersRouter);
29+
app.use("/api/posts", postsRouter);
30+
app.use("/api/authors", authorsRouter);
31+
32+
app.use((req, res, next) => {
33+
const error = new Error("Not found");
34+
error.statusCode = 404;
35+
logger.error(error);
36+
next(error);
37+
});
38+
39+
app.use(globalErrorHandler);
40+
41+
module.exports = app;

config/config.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const path = require("node:path");
2+
require("dotenv").config({path: path.resolve(__dirname, "./.env")});
3+
4+
const PORT = process.env.PORT || 3001;
5+
const DB = process.env.DB;
6+
const SECRET = process.env.SECRET;
7+
const SESSION_SECRET = process.env.SESSION_SECRET;
8+
const SESSION_NAME = process.env.SESSION_NAME;
9+
10+
module.exports = {
11+
PORT,
12+
DB,
13+
SECRET,
14+
SESSION_SECRET,
15+
SESSION_NAME,
16+
};

errors/globalErrorHandler.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const globalErrorHandler = (err, req, res, next) => {
2+
err.name = err.name || "Error";
3+
err.statusCode = err.statusCode || 500;
4+
err.message = err.message || res.statusMessage;
5+
6+
res.status(err.statusCode).json({
7+
name: err.name,
8+
statusCode: err.statusCode,
9+
message: err.message,
10+
stack: err.stack,
11+
});
12+
};
13+
14+
module.exports = globalErrorHandler;

index.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
const https = require("node:https");
2+
const fs = require("node:fs");
3+
const config = require("./config/config");
4+
const {connectToDatabase} = require("./utils/db");
5+
const logger = require("./logger/logger");
6+
const app = require("./app");
7+
8+
const httpsServer = https.createServer(
9+
{
10+
key: fs.readFileSync("./certs/localhost-key.pem"),
11+
cert: fs.readFileSync("./certs/localhost.pem"),
12+
},
13+
app
14+
);
15+
16+
const startServer = async () => {
17+
await connectToDatabase();
18+
httpsServer.listen(config.PORT, () => {
19+
logger.info(`Server running on https://localhost:${config.PORT}`);
20+
});
21+
};
22+
23+
startServer();

0 commit comments

Comments
 (0)