DWD Final – Zen Dog

I leaned something today:

Skills learned
  • use libraries which I’ve learned in this course to create a single web app.

View Project

View Source Code

  • webpack
module.exports = {
  entry: './api/index.js',
  output: {
    path: __dirname + '/public',
    filename: 'bundle.js'
  },
  module: {
    loaders: [
      {
        test: /.js$/,
        loader: 'babel-loader'
      }
    ]
  }
};
  • nodemon
"scripts": {
    "test": "echo "Error: no test specified" && exit 1",
    "start": "nodemon --exec babel-node serverHttps3000.js --ignore public/ & webpack -d --watch"
  },
  • babel
$ babel-node serverHttps3000.js
  • Mlab database
var mongojs = require('mongojs');
var config = require('./config.js');
var db = mongojs(config.username + ':' + config.passsword + "@ds043350.mlab.com:43350/testdata01");
var updateDuration = (name, newDuration) => {
  db.top10.update({
    name: name
  }, {
    $set: {
      duration: newDuration
    }
  }, () => console.log(`${name}'s new duration is ${newDuration}`))
}

var StoreNewTopDog = (obj) => {
  db.top10.save(obj, (err, saved) => {
    let log = (err || !saved)
      ? 'date didn't save'
      : obj + 'saved to top10';
    console.log(log);
  })
}
  • module.exports
export default router;
  • create api routers
import express from 'express';
import config from '../config';

const mongojs = require('mongojs');
const db = mongojs(config.username + ':' + config.passsword + "@ds043350.mlab.com:43350/testdata01");
const router = express.Router();

router.get('/top10', (req, res) =>
{
  db.top10.find({}, (err, data) => {
    if (err || !data) {
      console.log('date didn't find');
    } else {
      res.send(data);
    };
  });
});

export default router;
import apiRouter from './api';
app.use('/api', apiRouter);
  • use forever to run scripts
$ forever start server.js
$ forever stopall
// not using forever since I'm using babel.
  • https
const https = require('https');
const fs = require('fs'); // Using the filesystem module

const credentials = {
  key: fs.readFileSync('/etc/letsencrypt/live/hellidea.com/privkey.pem'),
  cert: fs.readFileSync('/etc/letsencrypt/live/hellidea.com/fullchain.pem')
};

const httpsServer = https.createServer(credentials, app);

httpsServer.listen(3000);
  • Ajax and jQuery
  var getRank = () => {
    $.ajax({
      url: "./api/top10",
      dataType: 'json',
      success: (data) => {
        console.log(data);
        let sortedKeys = SortDogs(data);
        console.log(sortedKeys);
        $('#ranks').empty();
        for (let k = 0; k < 10; k++) {
          let key = sortedKeys[k];
          for (let i = 0; i < data.length; i++) {
            if (data[i].duration === key) {
              let dura = durationStringify(data[i].duration);
              $('#ranks').append(`<li>   ${data[i].name}   :   ${dura} </li>`);
            }
          }
        }
      },
      error: () => {
        $('#ranks').append(`database error`);
      }
    });
  };
  • ejs

this is what in my index.ejs:

<%- include('header') -%>
  <%- include('sketch') -%>
  <%- include('outputSpecial') -%>
  <%- include('ranking') -%>
    <%- include('footer') -%>

Libraries used:
  • express

  • socket.io

  • ejs

  • mongojs

  • p5.js

  • jQuery

Leave a Reply

Your email address will not be published. Required fields are marked *