Skip to content

Commit

Permalink
refactor: create a function to set default headers
Browse files Browse the repository at this point in the history
  • Loading branch information
diogo405 committed Oct 1, 2022
1 parent a77f07b commit 5ace19e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 26 deletions.
12 changes: 3 additions & 9 deletions routes/dutch.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,12 @@ const router = express.Router();
import { pronounce } from "node-pronounce";
import { readFileSync } from "fs";
import { randomArrayItem } from "random-array-item";
import { setDefaultHeaders } from "../utils/headers.js";
var dutchRandomWord = [];
const dutchWordsData = JSON.parse(readFileSync(new URL("../data/dutch/words.json", import.meta.url)));

router.get("/", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
res.header("Access-Control-Allow-Methods", "GET");
res.header("X-Frame-Options", "DENY");
res.header("X-XSS-Protection", "1; mode=block");
res.header("X-Content-Type-Options", "nosniff");
res.header("Strict-Transport-Security", "max-age=63072000");
res.setHeader("Content-Type", "application/json");
setDefaultHeaders(res);

if (dutchRandomWord.length > 0) {
dutchRandomWord = [];
Expand All @@ -38,4 +32,4 @@ router.get("/", function (req, res) {
res.send(JSON.stringify(dutchRandomWord, null, 2));
});

export default router;
export default router;
21 changes: 4 additions & 17 deletions routes/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,13 @@ import axios from "axios";
import * as cheerio from "cheerio";
import { pronounce } from "node-pronounce";
import randomUseragent from "random-useragent";
import { setDefaultHeaders } from "../utils/headers.js";
const rua = randomUseragent.getRandom();
var wordOfDay = [];
const baseUrl = 'https://randomword.com';

router.get("/", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
res.header("Access-Control-Allow-Methods", "GET");
res.header("X-Frame-Options", "DENY");
res.header("X-XSS-Protection", "1; mode=block");
res.header("X-Content-Type-Options", "nosniff");
res.header("Strict-Transport-Security", "max-age=63072000");
res.setHeader("Content-Type", "application/json");
setDefaultHeaders(res);

axios({
method: "GET",
Expand Down Expand Up @@ -70,14 +64,7 @@ router.get("/", function (req, res) {
});

router.get("/:pos", function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
res.header("Access-Control-Allow-Methods", "GET");
res.header("X-Frame-Options", "DENY");
res.header("X-XSS-Protection", "1; mode=block");
res.header("X-Content-Type-Options", "nosniff");
res.header("Strict-Transport-Security", "max-age=63072000");
res.setHeader("Content-Type", "application/json");
setDefaultHeaders(res);

const partOfSpeech = req.params.pos;

Expand Down Expand Up @@ -132,4 +119,4 @@ router.get("/:pos", function (req, res) {
});
});

export default router;
export default router;
10 changes: 10 additions & 0 deletions utils/headers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const setDefaultHeaders = (response) => {
response.header("Access-Control-Allow-Origin", "*");
response.header('Access-Control-Allow-Headers', 'Access-Control-Allow-Headers,Content-Type,Access-Control-Allow-Methods, Authorization, X-Requested-With');
response.header("Access-Control-Allow-Methods", "GET");
response.header("X-Frame-Options", "DENY");
response.header("X-XSS-Protection", "1; mode=block");
response.header("X-Content-Type-Options", "nosniff");
response.header("Strict-Transport-Security", "max-age=63072000");
response.setHeader("Content-Type", "application/json");
}

0 comments on commit 5ace19e

Please sign in to comment.