Create more utils: logger and responder
This commit is contained in:
parent
d68d780951
commit
561e95c255
|
@ -5,8 +5,10 @@
|
||||||
"main": "./dist/main.js",
|
"main": "./dist/main.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "echo \"Error: no test specified\" && exit 1",
|
"test": "echo \"Error: no test specified\" && exit 1",
|
||||||
"build": "tsc -p .",
|
"clean": "rm -rf ./dist",
|
||||||
"start": "node ./dist/main.js"
|
"build": "pnpm clean && tsc -p .",
|
||||||
|
"start": "node ./dist/main.js",
|
||||||
|
"clean-start": "pnpm build && pnpm start"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"keywords": [],
|
||||||
"author": "",
|
"author": "",
|
||||||
|
|
36
src/main.ts
36
src/main.ts
|
@ -1,13 +1,15 @@
|
||||||
import chalk from "chalk";
|
|
||||||
import fs from "fs";
|
import fs from "fs";
|
||||||
import http from "http";
|
import http from "http";
|
||||||
import dotenv from "dotenv";
|
import dotenv from "dotenv";
|
||||||
import { Review, reviewSchema } from "./types";
|
import { Review, reviewSchema } from "./types";
|
||||||
import { checkFile } from "./utils";
|
import { checkFile, emp, Logger, Responder } from "./utils";
|
||||||
dotenv.config();
|
dotenv.config();
|
||||||
|
|
||||||
const origin = chalk.bold(chalk.yellow("main.js: "));
|
const logger = new Logger();
|
||||||
|
const response = new Responder();
|
||||||
|
// @ts-ignore
|
||||||
const port = +process.env.PORT ?? 8080;
|
const port = +process.env.PORT ?? 8080;
|
||||||
|
const host = emp(`http://localhost:${port}`);
|
||||||
const contentType = { "Content-Type": "application/json" };
|
const contentType = { "Content-Type": "application/json" };
|
||||||
|
|
||||||
http
|
http
|
||||||
|
@ -15,6 +17,7 @@ http
|
||||||
const isPost = req.method === "POST";
|
const isPost = req.method === "POST";
|
||||||
|
|
||||||
req.on("data", async (chunk) => {
|
req.on("data", async (chunk) => {
|
||||||
|
const sender = req.headers["user-agent"];
|
||||||
let data: Review[] = checkFile("data.json", "utf8");
|
let data: Review[] = checkFile("data.json", "utf8");
|
||||||
let temp: any;
|
let temp: any;
|
||||||
|
|
||||||
|
@ -29,10 +32,7 @@ http
|
||||||
.validate(temp)
|
.validate(temp)
|
||||||
.then((valid) => {
|
.then((valid) => {
|
||||||
req.on("end", () => {
|
req.on("end", () => {
|
||||||
console.log(
|
logger.log(`${emp(sender)} sent: ${valid}`);
|
||||||
origin + `${chalk.green(req.headers["user-agent"])} sent:`,
|
|
||||||
valid,
|
|
||||||
);
|
|
||||||
|
|
||||||
data.push(valid);
|
data.push(valid);
|
||||||
fs.writeFileSync(
|
fs.writeFileSync(
|
||||||
|
@ -40,31 +40,31 @@ http
|
||||||
JSON.stringify(data, null, 2),
|
JSON.stringify(data, null, 2),
|
||||||
);
|
);
|
||||||
|
|
||||||
res.writeHead(200, contentType);
|
res.writeHead(201, contentType);
|
||||||
res.write("OK");
|
res.write(response.success("Review was sent"));
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.catch((error) => {
|
.catch((err) => {
|
||||||
console.error(error);
|
logger.error(err);
|
||||||
|
|
||||||
res.writeHead(415, contentType);
|
res.writeHead(415, contentType);
|
||||||
res.write(JSON.stringify({ error: "Invalid content type." }));
|
res.write(JSON.stringify({ response: "error", message: "Invalid content-type." }));
|
||||||
res.end();
|
res.end();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
req.on("end", () => {
|
req.on("end", () => {
|
||||||
|
const sender = req.headers["user-agent"];
|
||||||
if (!isPost) {
|
if (!isPost) {
|
||||||
console.log(
|
logger.warn(`${emp(sender)} sent no data.`)
|
||||||
origin + `${chalk.green(req.headers["user-agent"])} sent no body.`,
|
|
||||||
);
|
res.writeHead(400, contentType);
|
||||||
res.writeHead(415, contentType);
|
res.write(response.error("Invalid content-type"));
|
||||||
res.write(JSON.stringify({ error: "Invalid content type." }));
|
|
||||||
res.end();
|
res.end();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
.listen(port || 8080);
|
.listen(port || 8080);
|
||||||
|
|
||||||
console.log(origin + `Server started on port ${chalk.green(port)}`);
|
logger.log(`Server started on ${host}`);
|
||||||
|
|
|
@ -1,2 +1,3 @@
|
||||||
|
export * from "./functions";
|
||||||
export * from "./logger";
|
export * from "./logger";
|
||||||
export * from "./utils";
|
export * from "./responder";
|
||||||
|
|
|
@ -12,19 +12,19 @@ export class Logger {
|
||||||
this._main = chalk.bold.gray("[Simple Review Server] ");
|
this._main = chalk.bold.gray("[Simple Review Server] ");
|
||||||
}
|
}
|
||||||
|
|
||||||
log(text: any, args?: any): void {
|
public log(text: any, args?: any): void {
|
||||||
args
|
args
|
||||||
? console.log(this._main + text, args)
|
? console.log(this._main + text, args)
|
||||||
: console.log(this._main + text);
|
: console.log(this._main + text);
|
||||||
}
|
}
|
||||||
|
|
||||||
warn(text: any, args?: any): void {
|
public warn(text: any, args?: any): void {
|
||||||
args
|
args
|
||||||
? console.warn(this._wrn + this._main + text, args)
|
? console.warn(this._wrn + this._main + text, args)
|
||||||
: console.warn(this._wrn + this._main + text);
|
: console.warn(this._wrn + this._main + text);
|
||||||
}
|
}
|
||||||
|
|
||||||
error(text: any, args?: any): void {
|
public error(text: any, args?: any): void {
|
||||||
args
|
args
|
||||||
? console.error(this._err + this._main + text, args)
|
? console.error(this._err + this._main + text, args)
|
||||||
: console.error(this._err + this._main + text);
|
: console.error(this._err + this._main + text);
|
||||||
|
|
17
src/utils/responder.ts
Normal file
17
src/utils/responder.ts
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
export class Responder {
|
||||||
|
constructor() {}
|
||||||
|
|
||||||
|
public success(message: string): string {
|
||||||
|
return JSON.stringify({
|
||||||
|
response: "success",
|
||||||
|
message: message
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public error(message: string): string {
|
||||||
|
return JSON.stringify({
|
||||||
|
response: "error",
|
||||||
|
message: message
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,7 +11,7 @@
|
||||||
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
// "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
|
||||||
|
|
||||||
/* Language and Environment */
|
/* Language and Environment */
|
||||||
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
"target": "es2021", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
|
||||||
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
// "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
|
||||||
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
// "jsx": "preserve", /* Specify what JSX code is generated. */
|
||||||
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
// "experimentalDecorators": true, /* Enable experimental support for legacy experimental decorators. */
|
||||||
|
|
Loading…
Reference in a new issue