Skip to content

Commit

Permalink
fix some code and change readme
Browse files Browse the repository at this point in the history
  • Loading branch information
akastav committed Jun 25, 2021
1 parent 730a7a6 commit 76aaf7a
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 74 deletions.
24 changes: 9 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@

## :floppy_disk: Setup - Backend

* Change to `/server` directory
* Install dependencies using `npm i`
* Install [nodemon](https://www.npmjs.com/package/nodemon) globally if you don't already have it
* Install [PostgreSQL](https://www.postgresql.org/) & run it (requires the password you created during installation)
* Add postgresql database & weather API access credentials to .env file
* Run `nodemon server` for a dev server on port 5000
* Add postgresql database & weather API access credentials (3815f2d2474d4bf0fd7527bd628f45cb) to .env file
* Run `start.sh` for a database migrations and dev server on port 5000


## :computer: Code Examples - Backend
## Environment variables
Set postgresql url:

* [Static method used](https://javascript.info/static-properties-methods) to add city to database list of cities.
`DATABASE_URL=postgres://localhost:5432/database`

set api key for access to WEATHER API:

`WEATHER_API_KEY: '3815f2d2474d4bf0fd7527bd628f45cb`

```javascript
// callback function - if error return it to callback. If no error then return rows (empty set)
static insert (city, callback) {
db.query('INSERT INTO cities (city_name) VALUES ($1)', [city], (err, res) => {
if (err.error)
return callback(err);
callback(res);
});
}
```
13 changes: 11 additions & 2 deletions api/cities.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
const express = require('express');
const cors = require('cors');
const Cities = require('../models/cities');

const router = express.Router();


// use it before all route definitions
router.use(cors({origin: 'http://localhost:3000'}));

var corsOptions = {
origin: 'http://localhost:3000',
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}
// ROUTES

// GET method. Use Cities model and retrieve list of cities as JSON
router.get('/', (req, res) => {
router.get('/', cors(corsOptions), (req, res) => {
Cities.retrieveAll((err, cities) => {
if (err)
return res.json(err);
Expand All @@ -16,7 +25,7 @@ router.get('/', (req, res) => {

// capture city variable
// if error return as json data to frontend
router.post('/', (req, res) => {
router.post('/', cors(corsOptions), (req, res) => {
const city = req.body.city;

Cities.insert(city, (err, result) => {
Expand Down
8 changes: 7 additions & 1 deletion api/weather.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
const express = require('express');
const Weather = require('../models/weather');
const cors = require('cors');

const router = express.Router();

var corsOptions = {
origin: 'http://localhost:3000',
optionsSuccessStatus: 200 // some legacy browsers (IE11, various SmartTVs) choke on 204
}

// get weather for a chosen city
router.get('/:city', (req, res) => {
router.get('/:city', cors(corsOptions), (req, res) => {
const city = req.params.city;

Weather.retrieveByCity(city, (err, weather) => {
Expand Down
7 changes: 7 additions & 0 deletions migrations/1624533032349_initial.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
exports.up = (pgm) => {
pgm.createTable('cities', {
id: 'id',
city_name: { type: 'varchar(50)', notNull: true },
})
pgm.createIndex('cities', 'id')
}
Loading

0 comments on commit 76aaf7a

Please sign in to comment.