Skip to content

Commit

Permalink
migrate to TS
Browse files Browse the repository at this point in the history
  • Loading branch information
f.marayeu committed Apr 17, 2024
1 parent bd7b8e9 commit 37a627d
Show file tree
Hide file tree
Showing 15 changed files with 876 additions and 185 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
.vscode
node_modules
node_modules
.idea
27 changes: 12 additions & 15 deletions index.js → index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,28 @@
import express from 'express'
import express, { Express, Request, Response } from "express";
import fs from 'fs';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import { getImagePathByName } from './src/picture.js'
import { getImagePathByName } from "./src/picture";

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const PATH_TO_IMAGES = './images';
const PICTURES = fs.readdirSync(PATH_TO_IMAGES);
const PATH_TO_IMAGES: string = './images';
const PICTURES: string[] = fs.readdirSync(PATH_TO_IMAGES);

const app = express();
const app: Express = express();
const appPort: number = 3001;

app.use(express.json());

app.get('/random_picture', (req, res) => {
app.get('/random_picture', (req: Request, res: Response) => {
console.log(__dirname);
res.sendFile(getImagePathByName(PATH_TO_IMAGES, PICTURES), { root: __dirname });
res.sendFile(getImagePathByName(PATH_TO_IMAGES, PICTURES, undefined), { root: __dirname });
})

app.post('/picture', (req, res) => {
app.post('/picture', (req: Request, res: Response) => {
const requestedPicture = req.body.picture;

if (!requestedPicture) {
return res.sendStatus(400);
}

let pathToImage;
let pathToImage: string = '';
try{
pathToImage = `${__dirname}/${getImagePathByName(PATH_TO_IMAGES, PICTURES, requestedPicture)}`;
} catch (error) {
Expand All @@ -37,7 +34,7 @@ app.post('/picture', (req, res) => {
res.sendStatus(400);
}

fs.access(pathToImage, fs.constants.F_OK, (err) => {
fs.access(pathToImage, fs.constants.F_OK, (err: Error | null) => {
if (err) {
res.sendStatus(404);
} else {
Expand All @@ -47,4 +44,4 @@ app.post('/picture', (req, res) => {

})

app.listen(3001, () => console.log('API Server is up and running...'));
app.listen(appPort, () => console.log('API Server is up and running...'));
4 changes: 4 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
};
Loading

0 comments on commit 37a627d

Please sign in to comment.