Specify whether the error was because of the server or a client
This commit is contained in:
parent
8d6572c631
commit
34bcb07a1a
10
src/main.ts
10
src/main.ts
|
@ -2,7 +2,7 @@ import fs from "fs";
|
|||
import http from "http";
|
||||
import dotenv from "dotenv";
|
||||
import { Review, reviewSchema } from "./types";
|
||||
import { checkFile, emp, Logger, Responder } from "./utils";
|
||||
import { bold, checkFile, emp, Logger, Responder } from "./utils";
|
||||
dotenv.config();
|
||||
|
||||
const logger = new Logger("Simple Review Server");
|
||||
|
@ -15,7 +15,7 @@ const contentType = { "Content-Type": "application/json" };
|
|||
http
|
||||
.createServer((req, res) => {
|
||||
const isPost = req.method === "POST";
|
||||
const sender = emp(req.headers["user-agent"]);
|
||||
const sender = `${bold(emp("Client:"))} ${emp(req.headers["user-agent"])}`;
|
||||
|
||||
req.on("data", async (chunk) => {
|
||||
|
||||
|
@ -26,7 +26,7 @@ http
|
|||
try {
|
||||
temp = JSON.parse(chunk);
|
||||
} catch (err) {
|
||||
logger.error("Recieved chunk was not valid JSON!");
|
||||
logger.error(`${sender} <~ Recieved chunk was not valid JSON!`);
|
||||
|
||||
res.writeHead(400, contentType);
|
||||
res.write(response.JsonError("recieved chunk was not valid JSON"));
|
||||
|
@ -38,7 +38,7 @@ http
|
|||
.validate(temp)
|
||||
.then((valid) => {
|
||||
req.on("end", () => {
|
||||
logger.log(`${sender} sent:`, valid);
|
||||
logger.log(`${sender} ~>`, valid);
|
||||
|
||||
data.push(valid);
|
||||
fs.writeFileSync(
|
||||
|
@ -52,7 +52,7 @@ http
|
|||
});
|
||||
})
|
||||
.catch((err) => {
|
||||
logger.error(err);
|
||||
logger.error(`${sender} <~ ${err}`);
|
||||
|
||||
res.writeHead(400, contentType);
|
||||
res.write(response.error(err));
|
||||
|
|
|
@ -34,3 +34,7 @@ export class Logger {
|
|||
export const emp = chalk.green;
|
||||
export const wrn = chalk.yellow;
|
||||
export const err = chalk.red;
|
||||
|
||||
export const bold = chalk.bold;
|
||||
export const itl = chalk.italic;
|
||||
export const udln = chalk.underline;
|
||||
|
|
Loading…
Reference in a new issue