Coding Journal

Jordan Vidrine

Daily Coding Journal

An experienced business owner + musician with a demonstrated history of management in the Energy sector and an insatiable appetite for learning new and complex skills. Transitioning from an ownership role & seeking employment in Web Development as a Jr Full Stack Developer.

June 24th 2019

Started the day working on the second 100 days of css challenge and completed it. It took about an hour, but I really feel like I am already getting used to the css animations syntax. Got to play around with the clip path CSS as well which was fun. Here is the solution:

See the Pen 100 Dats of CSS #2 by Jordan Vidrine (@jordanvidrine) on CodePen.

Node/Express/API Learning

Started to try my hand at working with Node and Express on my own without a walkthrough or tutorial. It was a bit slow going but I got the hang of it after a couple of hours of noodling around. I was able to get a basic website up, establish to endpoints for an express app, and then request those endpoints from my front-end.

Here is that back-end code:


const request = require('request');
const path = require('path') //lets you specify path directorys
const express = require('express');
const hbs = require('hbs'); //for partials

const app = express();

//path names
const publicDirectoryPath = path.join(__dirname, '../public')
const viewsPath = path.join(__dirname, '../templates/views')
const partialsPath = path.join(__dirname, '../templates/partials')
const scriptsPath = path.join(__dirname, '../node_modules/')

//specifies what directory to use for "/" get path
app.use(express.static(publicDirectoryPath))

//specifies the scripts directory
app.use('/scripts', express.static(scriptsPath))

app.set('view engine', 'hbs');
app.set('views', viewsPath);
hbs.registerPartials(partialsPath)

app.get('/',(req,res) => {
  res.render('index', {
    title: 'Stock Info App'
  })
})

function getStock(symbol, callback) {
  request({url: STOCK_URL + symbol + STOCK_KEY, json: true}, (err,res) => {
    if (err) {
      console.log(err)
      return callback('ERROR', undefined)
    } else {
      if (res.body.Message || res.body.message) {
        return callback(res.body.Message || res.body.message, undefined)
      } else {
        let data = res.body;
        callback(undefined, data)
      }
    }
  })
}

function getStockHistory(symbol, from, to, callback) {
  request({url: STOCK_HISTORY_URL +
    symbol + STOCK_KEY +
    `&date_from=${from}&date_to=${to}`, json: true }, (err,res) => {
      if (err) {
         return callback('ERROR!', undefined)
      } else {
        if (res.body.Message || res.body.message) {
          return callback(res.body.Message || res.body.message, undefined)
        } else {
          let data = res.body;
          callback(undefined, data)
        }
      }
    })
}

app.get('/stock', (req,res) => {
  if (!req.query.stock) return res.send({error:'You must provide a stock symbol!'})

  getStock(req.query.stock, (error, data) => {
    if (error) return res.send({error});
    console.log(data)
    res.send({
      data: data.data[0],
    })
  })
});

app.get('/stock-history', (req,res) => {
  if (!req.query.stock) return res.send({error:'You must provide a stock symbol!'})

  getStockHistory(req.query.stock,req.query.date_from,req.query.date_to, (error, data) => {
    if (error) return res.send({error});
    res.send({
      data: data,
    })
  })
});

app.listen(3000, () => {
  console.log('Server is up on port 3000')
})

Back to blog...