Skip to content

Commit

Permalink
[Task] #10 validating incoming parameter and logging errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Type-Style committed Jan 20, 2024
1 parent 3c5a6a2 commit 57fbd75
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
10 changes: 3 additions & 7 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ require('module-alias/register');
import { config } from 'dotenv';
import express from 'express';
import hpp from 'hpp';
import fs from 'fs';
import path from 'path';
import writeRouter from '@src/controller/write';
import path from 'path';
import logger from '@src/scripts/logger';

// configurations
config();
Expand All @@ -23,9 +23,5 @@ app.use('/', express.static(path.join(__dirname, 'httpdocs')))

// init server
app.listen(80, () => {
const date = new Date().toLocaleString('de-DE', { hour12: false });
const logPath = path.join(__dirname, 'httpdocs', 'log.txt');
fs.appendFileSync(logPath, `Express Server started: ${date} \n`);

console.log(`Server running //localhost:80`);
logger.log(`Server running //localhost:80`);
});
5 changes: 4 additions & 1 deletion src/controller/write.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import express from 'express';
import entry from '@src/models/entry';
import { validationResult } from 'express-validator';
import logger from '@src/scripts/logger';


const router = express.Router();
Expand All @@ -10,7 +11,9 @@ router.use(entry.validate);
router.get('/', (req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
res.status(422).json({ errors: errors.array() });
const errorAsJson = { errors: errors.array() };
logger.log(JSON.stringify(errorAsJson));
res.status(422).json(errorAsJson);
return next();
}

Expand Down
12 changes: 8 additions & 4 deletions src/models/entry.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { kMaxLength } from 'buffer';
import { Request, Response, NextFunction } from 'express';
import { Request, Response} from 'express';
import { query } from 'express-validator';

const entry = {
Expand All @@ -13,14 +12,19 @@ const entry = {
query('lon').custom(checkNumber(-180, 180)),
query('timestamp').custom(checkTime),
query('hdop').custom(checkNumber(0, 100)),
// TODO altitude, speed, heading
query('altitude').custom(checkNumber(0, 10000)),
query('speed').custom(checkNumber(0, 300)),
query('heading').custom(checkNumber(0, 360)),
]

}

function checkNumber(min:number, max:number) {
return (value:string) => {
if (value.length > 11) {
if (!value) {
throw new Error('is required');
}
if (value.length > 12) {
throw new Error('Should have a maximum of 11 digits');
}

Expand Down
14 changes: 14 additions & 0 deletions src/scripts/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// primitive text logger
import fs from 'fs';
import path from 'path';

const logPath = path.resolve(__dirname, '../httpdocs', 'log.txt');
const date = new Date().toLocaleString('de-DE', { hour12: false });

export default {
log: (message:string|JSON) => {
fs.appendFileSync(logPath, `${date} \t|\t ${message} \n`);
console.log(message);

}
}

0 comments on commit 57fbd75

Please sign in to comment.