-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
49 lines (47 loc) · 1.33 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const express = require("express");
const Datastore = require("nedb");
const fetch = require("node-fetch");
require("dotenv").config();
const app = express();
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`listening at ${port}`));
app.use(express.static("public"));
app.use(express.json({ limit: "1mb" }));
const database = new Datastore("database.db");
database.loadDatabase();
app.get("/api", (request, response) => {
database.find({}, (err, data) => {
if (err) {
response.end();
return;
}
response.json(data);
});
});
app.post("/api", (request, response) => {
const data = request.body;
console.log(data.username);
const timestamp = Date.now();
data.timestamp = timestamp;
database.insert(data);
response.json(data);
});
app.get("/location/:latlon", async (request, response) => {
const latlon = request.params.latlon.split(",");
const latitude = latlon[0];
const longitude = latlon[1];
var apikey = process.env.API_KEY;
var api_url = "https://api.opencagedata.com/geocode/v1/json";
var request_url =
api_url +
"?" +
"key=" +
apikey +
"&q=" +
encodeURIComponent(latitude + "," + longitude) +
"&pretty=1" +
"&no_annotations=1";
const fetch_response = await fetch(request_url);
const json = await fetch_response.json();
response.json(json);
});