server format commit

This commit is contained in:
Stefan Forstenlechner 2022-04-09 20:28:35 +02:00
parent 952d4dc68e
commit a50dbd0d36
7 changed files with 3874 additions and 69 deletions

View File

@ -0,0 +1,34 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"airbnb-base",
"plugin:prettier/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"import/extensions": [
"error",
"ignorePackages",
{
"ts": "never",
"tsx": "never"
}
]
},
"settings": {
"import/resolver": {
"typescript": {
}
}
}
}

View File

@ -0,0 +1,2 @@
node_modules
dist

File diff suppressed because it is too large Load Diff

View File

@ -8,18 +8,30 @@
"build-server": "npx tsc",
"start-server": "node dist/app.js",
"watch-server": "npx nodemon src/app.ts",
"run-server": "npm run build-server && npm run start-server"
"run-server": "npm run build-server && npm run start-server",
"lint": "eslint src/*",
"lint:fix": "eslint --fix src/*",
"format": "prettier --write \"**/*.+(ts|tsx)\""
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/express": "^4.17.13",
"@typescript-eslint/eslint-plugin": "^5.18.0",
"@typescript-eslint/parser": "^5.18.0",
"eslint": "^8.13.0",
"eslint-config-airbnb-base": "^15.0.0",
"eslint-config-prettier": "^8.5.0",
"eslint-import-resolver-typescript": "^2.7.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.0.0",
"prettier": "^2.6.2",
"typescript": "^4.6.3"
},
"dependencies": {
"image-size": "^1.0.1",
"express": "^4.17.3",
"image-size": "^1.0.1",
"nodemon": "^2.0.15",
"ts-node": "^10.7.0"
}

View File

@ -1,4 +1,4 @@
import express from 'express';
import express from "express";
import * as fs from "fs";
import * as path from "path";
import sizeOf from "image-size";
@ -9,28 +9,30 @@ const app = express();
const PORT = process.env.PORT || 3001;
app.use('/images', express.static(publicPath))
app.use("/images", express.static(publicPath));
app.use(express.static('../picture-gallery-client/build'));
app.use(express.static("../picture-gallery-client/build"));
app.get("/api(/*)?", (req, res) => {
const path = req.path.substring(4);
const requestedPath = req.path.substring(4);
const dirents = fs.readdirSync(publicPath + path, {withFileTypes: true}); // TODO: error handling?
const dirents = fs.readdirSync(publicPath + requestedPath, {
withFileTypes: true,
});
const normalizedPath = path === '/' ? "" : path;
const normalizedPath = requestedPath === "/" ? "" : requestedPath;
const images: Image[] = dirents
.filter(f => f.isFile())
.map(f => {
const dimensions = sizeOf(`${publicPath}${path}/${f.name}`)
.filter((f) => f.isFile())
.map((f) => {
const dimensions = sizeOf(`${publicPath}${requestedPath}/${f.name}`);
return {
src: `/images${normalizedPath}/${f.name}`,
width: dimensions.width,
height: dimensions.height,
}
};
});
res.json(a<Folder>({images: images}));
res.json(a<Folder>({ images }));
});
app.get("/directories", (req, res) => {
@ -38,10 +40,13 @@ app.get("/directories", (req, res) => {
});
// All other GET requests not handled before will return our React app
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '../../picture-gallery-client/build/index.html'));
app.get("*", (req, res) => {
res.sendFile(
path.resolve(__dirname, "../../picture-gallery-client/build/index.html")
);
});
app.listen(PORT, () => {
// eslint-disable-next-line no-console
return console.log(`Express is listening at http://localhost:${PORT}`);
});

View File

@ -1,8 +1,8 @@
import {Folders} from "./models";
import fs from "fs";
import * as path from "path";
import { Folders } from "./models";
export const publicPath = '../public'
export const publicPath = "../public";
export const walk = (dirPath: string): Folders => {
const dirEnts = fs.readdirSync(publicPath + dirPath, { withFileTypes: true });
@ -10,7 +10,9 @@ export const walk = (dirPath: string): Folders => {
return {
name: path.basename(dirPath) || "Home",
fullPath: dirPath,
numberOfFiles: dirEnts.filter(f => f.isFile()).length,
children: dirEnts.filter(d => d.isDirectory()).map(d => walk(path.posix.join(dirPath, d.name)))
}
}
numberOfFiles: dirEnts.filter((f) => f.isFile()).length,
children: dirEnts
.filter((d) => d.isDirectory())
.map((d) => walk(path.posix.join(dirPath, d.name))),
};
};

View File

@ -1,20 +1,20 @@
export interface Image {
src: string,
width: number,
height: number
src: string;
width: number;
height: number;
}
export interface Folder {
images: Image[]
images: Image[];
}
export interface Folders {
name: string
fullPath: string
numberOfFiles: number
children: Folders[]
name: string;
fullPath: string;
numberOfFiles: number;
children: Folders[];
}
export const a = <T>(v: T): T => {
return v;
}
};