Skip to content

Commit f9f9a42

Browse files
committed
Created a util functions and dist to gitignore
1 parent 45cb297 commit f9f9a42

File tree

7 files changed

+46
-48
lines changed

7 files changed

+46
-48
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
/build
1313

1414
# misc
15+
/dist
1516
.env
1617
.DS_Store
1718
.env.local

dist/main.js

+3-21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/main.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

public/index.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
<head>
44
<title>Weather Backend API Server</title>
55
<link rel="stylesheet" href="styles.css" />
6+
<link
7+
rel="icon"
8+
href="https://www.shareicon.net/data/128x128/2015/10/06/112727_development_512x512.png"
9+
/>
610
</head>
711
<body>
8-
<h1>Hi Marcus</h1>
12+
<h1>Weather Backend API Server</h1>
13+
<h2>Powered by Express JS</h2>
914
</body>
1015
</html>

public/styles.css

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
text-align: center;
3+
}

src/main.ts

+4-25
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,17 @@
1-
import express, { Application, response } from "express";
2-
import path, { dirname } from "path";
3-
import { fileURLToPath } from "url";
1+
import express, { Application } from "express";
42
import morgan from "morgan";
5-
import axios from "axios";
6-
7-
function getPublicFolder(): string {
8-
const __filename = fileURLToPath(import.meta.url);
9-
const __dirname = dirname(__filename);
10-
return path.join(__dirname, "..", "public");
11-
}
12-
13-
async function get_location_data(req, res, next) {}
3+
import { getLocationData, getPublicFolderPath } from "./utils.js";
144

155
const app: Application = express();
166
const PORT: number = Number(process.env.PORT) || 8080;
17-
const PUBLIC_FOLDER: string = getPublicFolder(); // must be an abs path to serve website
18-
const API_KEY: string = process.env.API_KEY;
7+
const PUBLIC_FOLDER: string = getPublicFolderPath(); // must be an abs path to serve website
198

209
// Use static server to serve the website in the public folder;
2110
app.use(express.static(PUBLIC_FOLDER));
2211

2312
app.use(morgan("dev")); //Add logging for all endpoints
2413

25-
app.get("/weather-locs/:city", async (req, res, next) => {
26-
const city: string = req.params.city;
27-
try {
28-
const response = await axios.get(
29-
`https://geocode.maps.co/search?q=${city}&api_key=${API_KEY}`
30-
);
31-
res.status(response.status).send(response.data);
32-
} catch (err) {
33-
next(err);
34-
}
35-
});
14+
app.get("/weather-locs/:city", getLocationData);
3615

3716
//Add error handler for all endpoints
3817
app.use((err, req, res, next) => {

src/utils.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Request, Response, NextFunction } from "express";
2+
import path, { dirname } from "path";
3+
import { fileURLToPath } from "url";
4+
import axios from "axios";
5+
6+
const API_KEY: string = process.env.API_KEY;
7+
8+
export function getPublicFolderPath(): string {
9+
const __filename = fileURLToPath(import.meta.url); //abs path to filename
10+
const __dirname = dirname(__filename); //abs path to directory
11+
return path.join(__dirname, "..", "public");
12+
}
13+
14+
export async function getLocationData(
15+
req: Request,
16+
res: Response,
17+
next: NextFunction
18+
) {
19+
const city: string = req.params.city;
20+
try {
21+
const response = await axios.get(
22+
`https://geocode.maps.co/search?q=${city}&api_key=${API_KEY}`
23+
);
24+
res.status(response.status).send(response.data);
25+
} catch (err) {
26+
next(err);
27+
}
28+
}

0 commit comments

Comments
 (0)