forked from ciweala/INST377-FA2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
74 lines (59 loc) · 1.92 KB
/
server.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// These are our required libraries to make the server work.
/* eslint-disable no-console */
/* eslint-disable no-unused-vars */
import express from 'express';
import dotenv from 'dotenv';
import fetch from 'node-fetch';
import { open } from 'sqlite';
import sqlite3 from 'sqlite3';
dotenv.config();
const app = express();
const port = process.env.PORT || 3000;
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
app.use(express.static('public'));
app.use((req, res, next) => {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
next();
});
const dbSettings = {
filename: './tmp/database.db',
driver: sqlite3.Database
};
async function databaseInitialize(dbSettings) {
try {
const db = await open(dbSettings);
await db.exec(`CREATE TABLE IF NOT EXISTS restaurants (
id INTEGER PRIMARY KEY AUTOINCREMENT,
restaurant_name TEXT,
category TEXT)
`)
const data = await dataFetch();
const test = await db.get("SELECT * FROM restaurants")
console.log(test);
}
catch(e) {
console.log("Error loading Database");
console.log(e);
}
}
app.route('/api')
.get(async (req, res) => {
console.log('GET request detected');
const data = await fetch('https://data.princegeorgescountymd.gov/resource/umjn-t2iz.json');
const json = await data.json();
console.log('data from fetch', json);
res.json(json);
})
.post(async (req, res) => {
console.log('POST request detected');
console.log('Form data in res.body', req.body);
const data = await fetch('https://data.princegeorgescountymd.gov/resource/umjn-t2iz.json');
const json = await data.json();
console.log('data from fetch', json);
res.json(json);
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}!`);
});