Skip to content

Commit ace2679

Browse files
author
Niko
committedNov 19, 2019
Update syntax to be more clear
1 parent 1b90d8c commit ace2679

File tree

10 files changed

+53
-186
lines changed

10 files changed

+53
-186
lines changed
 

‎.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
PORT= # Port to be used by the app
2+
GOOGLE_API_KEY= # Google Maps API KEY

‎.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
package-lock.json
33

44
.DS_STORE
5+
.env

‎README.md

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# MLH Localhost Hometown Map
2+
23
This repository contains the source code for a map of the hometowns of anyone who attends the MLH Localhost workshop, How to Collaborate on Code Projects with GitHub. Participants learn best practice GitHub workflows, add their hometown to `locations.txt`, and see the giant community they're part of!
34

45
## How to participate in this workshop
@@ -39,11 +40,10 @@ Embedded JavaScript templates
3940

4041
### Google Maps API Key
4142

42-
To use the Google Maps JavaScript API, you will ned an API Key. The [API key](https://developers.google.com/maps/documentation/javascript/get-api-key) is used to track API requests associated with your project for usage and billing. To learn more about API keys, see the API Key Best Practices and the FAQs.
43+
To use the Google Maps JavaScript API, you will ned an API Key. The [API key](https://developers.google.com/maps/documentation/javascript/get-api-key) is used to track API requests associated with your project for usage and billing. To learn more about API keys, see the API Key Best Practices and the FAQs.
4344

4445
Read the [tutorial](docs/tutorial.md) and configure your [API Key](https://developers.google.com/maps/documentation/javascript/get-api-key)
4546

46-
4747
## Adding a new location
4848

4949
The App reads its locations from the `locations.txt` file located in the root folder. It will consider each line a different location, so the file should look something like this:
@@ -62,25 +62,27 @@ There is also a `locations.txt.example` file that can be used to test the app. C
6262

6363
To run this application locally, you'll need to:
6464

65-
* Clone this repository:
65+
- Clone this repository:
66+
6667
```
6768
git clone https://github.com/MLH/mlh-localhost-github.git
6869
cd localhost-github
6970
npm install
7071
```
7172

72-
* Set up environment variables:
73+
- Set up environment variables:
7374

7475
There is a `.env.example` file that can be used to configure the app. Simply create a copy named `.env`
7576

7677
```txt
7778
PORT= # Port to be used by the app
78-
API_KEY= # Google Maps API KEY
79+
GOOGLE_API_KEY= # Google Maps API KEY
7980
```
8081

8182
Now inside the root folder run:
83+
8284
```
8385
npm start
8486
```
8587

86-
Now you can access locally in your browser the address `http://localhost:3000` and see your locations map.
88+
Now you can access locally in your browser the address `http://localhost:3000` and see your locations map.

‎locations.txt

Lines changed: 2 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,170 +1,85 @@
11
Illinois, United States of America
2-
32
Goa, India
4-
53
Tehran, Iran
6-
74
Jalandhar, India
8-
95
Gatineau, Canada
10-
116
Lincoln, Pennsylvania
127
Changchun, China
138
Phoenix, Arizona
14-
159
Mississauga, Canada
16-
1710
Lake Hiawatha, New Jersey
18-
1911
Denver, Colorado
20-
2112
Moscow, Russia
22-
2313
Milwaukee, Wisconsin
24-
2514
New York, USA
26-
2715
Paris, France
28-
2916
Montreal, Canada
30-
3117
Goiania, Brazil
32-
3318
Vienna, Austria
34-
3519
Melbourne, Australia
36-
3720
Osaka, Japan
38-
3921
Calgary, Canada
40-
4122
Sydney, Australia
42-
4323
Vancouver, Canada
44-
4524
Toronto, Canada
46-
4725
Tokyo, Japan
48-
4926
Copenhagen, Denmark
50-
5127
Adelaide, Australia
52-
5328
Miami, Florida
54-
5529
Tulsa, Oklahoma
56-
5730
Queens, Ny
58-
5931
Bavaria, Germany
60-
6132
Jamaica, New York
62-
6333
Ambato, Ecuador
64-
6534
Binghamton, New York
66-
6735
South Ozone Park, New York
68-
6936
Queens, New York, USA
70-
7137
New York, New York
72-
7338
Santiago, Dominican Republic
74-
7539
Seattle, Washington
76-
7740
Gopaltila, Sylhet, Bangladesh
78-
7941
Manila, Philippines
80-
8142
Cali, Colombia
82-
8343
Brooklyn, New York
84-
8544
Long Island City, New York
86-
8745
Kowloon, Hong Kong
88-
8946
Irvington, New Jersey
90-
9147
Parsippany, New Jersey
92-
9348
Haworth, New Jersey
94-
9549
Dhaka, Bangladesh
96-
9750
Huntington, New York
98-
9951
New York, NY
100-
10152
Rockville, Maryland
102-
10353
Sylhet, Bangladesh
104-
10554
Hanmer, Canada
106-
10755
Forks of Salmon, California
108-
10956
Ahmedabad,India
110-
111-
simla,india
112-
57+
Simla,india
11358
Vadodara, India
114-
11559
Nakuru, Kenya
116-
11760
Anand,India
118-
11961
Patuakhali, Bangladesh
120-
12162
Ajax, Canada
122-
12363
Toronto, Canada
124-
12564
Burlington, Canada
126-
12765
Fontainebleau, France
128-
12966
Barisal, Bangladesh
130-
13167
Bagerhat, Bangladesh
132-
13368
Kingston, Canada
134-
13569
Trichy,India
136-
13770
Reading, Pennsylvania
138-
13971
Mysore, India
140-
14172
San Antonio, Texas
142-
14373
Plano, Illinois
144-
14574
Ciudad de Guatemala, Guatemala
146-
14775
Daegu, South Korea
148-
14976
Ho Chi Minh City, Vietnam
150-
15177
St. Catharines, Canada
152-
15378
Dzerzhinsk, Russia
154-
15579
Streamwood, Untied States of America
156-
15780
Quincy, Illinois
158-
15981
Oswego, Illinois
160-
16182
Maple Park, Illinois
162-
163-
Maple Park, Illinois
164-
16583
Elgin, Illinois
166-
167-
Warrenville, IL
168-
84+
Warrenville, IL
16985
Grayslake, Illinois
170-

‎package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "maps",
33
"version": "1.0.0",
4-
"description": "display participant locations in a map",
4+
"description": "Display participant locations in a map",
55
"main": "server.js",
66
"directories": {
77
"doc": "docs"
@@ -16,7 +16,7 @@
1616
"type": "git",
1717
"url": "git+https://github.com/MLH/localhost-github.git"
1818
},
19-
"author": "Halisson Bruno",
19+
"author": "Major League Hacking",
2020
"license": "MIT",
2121
"bugs": {
2222
"url": "https://github.com/MLH/localhost-github/issues"

‎src/addresses.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@ const fs = require("fs");
22

33
const DEFAULT_FILE_PATH = "../locations.txt";
44

5-
async function getAddresses(filePath = DEFAULT_FILE_PATH) {
6-
const fileContent = await fs.promises.readFile('locations.txt');
5+
const getAddresses = async function(filePath = DEFAULT_FILE_PATH) {
6+
const fileContent = await fs.promises.readFile("locations.txt");
77
return String(fileContent).split("\n");
8-
}
8+
};
99

10-
function getExampleAddresses() {
10+
const getExampleAddresses = function() {
1111
return [];
12-
}
12+
};
1313

1414
module.exports = {
1515
getAddresses,

‎src/locations.js

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,29 @@
11
const fs = require("fs");
22
const NodeGeocoder = require("node-geocoder");
33

4-
var options = {
4+
const options = {
55
provider: "google",
66
httpAdapter: "https",
77
apiKey: process.env.GOOGLE_API_KEY,
8+
sensor: true
89
};
910

10-
var geocoder = NodeGeocoder(options);
11+
const geocoder = NodeGeocoder(options);
1112

12-
async function getLocations(addresses) {
13-
const locations = await geocoder
14-
.batchGeocode(addresses)
15-
.filter(({ error }) => !error)
16-
.map(({ error, value: [result, ...results] }) => ({
17-
lat: result.latitude,
18-
lng: result.longitude
19-
}));
20-
return locations;
21-
}
13+
const getLocations = async function(addresses) {
14+
try {
15+
const locations = await geocoder
16+
.batchGeocode(addresses)
17+
.filter(({ error }) => !error)
18+
.map(({ error, value: [result, ...results] }) => ({
19+
lat: result.latitude,
20+
lng: result.longitude
21+
}));
22+
return locations;
23+
} catch (error) {
24+
console.error(error);
25+
}
26+
};
2227

2328
module.exports = {
2429
getLocations

‎src/server.js

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,31 @@ require("dotenv").config();
22
const express = require("express");
33
const morgan = require("morgan");
44
const path = require("path");
5+
56
const Addresses = require("./addresses");
67
const Locations = require("./locations");
78

89
const app = express();
9-
const PORT = process.env.PORT || 3000;
10+
const PORT = process.env.PORT || 3000;
1011
const googleMapsApiKey = process.env.GOOGLE_API_KEY;
1112

1213
app.use("/static", express.static("static")); // Exposes static folder to serve images and styles
1314
app.use(morgan("combined")); // Logs requests
1415
app.set("view engine", "ejs"); // Set default view engine
1516

1617
const start = async function() {
17-
console.info("Loading addresses.");
18+
// Let's load the addresses for the map.
1819
const addresses = await Addresses.getAddresses();
19-
console.info(
20-
"Converting addresses into locations. Please notice this may take a while if there are many addresses..."
21-
);
20+
21+
// Let's convert these addresses into locations.
2222
const locations = await Locations.getLocations(addresses);
23-
console.info("Starting application");
23+
24+
// We need to define the basic route for our application.
2425
app.get("/", function(req, res) {
25-
res.render("index", {
26-
locations,
27-
apiKey: googleMapsApiKey,
28-
});
26+
res.render("index", { locations, apiKey: googleMapsApiKey });
2927
});
3028

29+
// We start the application by listening to the defined PORT.
3130
app.listen(PORT, function() {
3231
console.log("Maps app listening on port " + PORT);
3332
});

0 commit comments

Comments
 (0)
Please sign in to comment.