{"size": 2038, "ext": "js", "max_stars_repo_path": "Java/Unknown/Java/tiy-raleigh-java/express-lecture-2/index.js", "max_stars_repo_name": "JamieBort/LearningDirectory", "max_stars_repo_stars_event_min_datetime": "2022-02-02T21:56:08.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-02T21:56:08.000Z", "max_issues_repo_path": "Java/Unknown/Java/tiy-raleigh-java/express-lecture-2/index.js", "max_issues_repo_name": "JamieBort/LearningDirectory", "max_issues_repo_issues_event_min_datetime": "2020-06-27T23:25:59.000Z", "max_issues_repo_issues_event_max_datetime": "2022-02-27T20:40:56.000Z", "max_forks_repo_path": "Java/Unknown/Java/tiy-raleigh-java/express-lecture-2/index.js", "max_forks_repo_name": "JamieBort/LearningDirectory", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": 27, "max_forks_count": null, "avg_line_length": 28.7042253521, "max_line_length": 101, "alphanum_fraction": 0.6820412169}
const express = require('express');
const mustacheExpress = require('mustache-express');
const bodyParser = require('body-parser');
const moment = require('moment');
const foods = require('./food');
const fs = require('fs');
const app = express();

// tell express to use mustache
app.engine('mustache', mustacheExpress());
app.set('views', './views');
app.set('view engine', 'mustache');

// tell express how to serve static files
app.use(express.static('public'));

// tell express to use the bodyParser middleware to parse form data
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// configure the webroot
app.get('/', function(req, res) {
  res.render('home', {
    formattedDate: moment().format('dddd, MMMM Do YYYY, h:mm:ss a'),
    foods: foods
  });
});

// show a particular food
app.get('/food/:id', function(req, res) {
  res.render('food', {
    foodItem: foods[req.params.id]
  });
});

// show a random number
app.get(
  // this means to listen for a request to http://..../randomNumber
  '/randomNumber/:max',
  // this is the code that gets run when the request comes in
  function(req, res) {
    // generate my random number
    let myRandomNumber = Math.round(Math.random() * req.params.max);

    // render the randomNumber template using the provided information
    res.render('randomNumber', {
      randomNumber: myRandomNumber
    });
  }
);

// this endpoint will show a form I can use to add foods
app.get('/foodForm', function(req, res) {
  res.render('foodForm');
});

// this handles the posted data from the form and creates a new food item
app.post('/addFood', function(req, res) {
  // get the food item details from the posted body data
  let foodItem = req.body;
  // set the id for this item (this is not important in the future, it just needs to be here for now)
  foodItem.id = foods.length;
  // store the food item in our array of foods
  foods.push(foodItem);

  res.send('Yay! You added a food item.');
});

// make express listen on port 3000
app.listen(3000);
