Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
'use strict';
var express = require('express'),
morgan = require('morgan'),
bodyParser = require('body-parser');
var tasksArray = ['Groceries', 'Take the dog out', 'Something else'];
// Create the express application object
var app = express();
app.set('view engine', 'pug');
app.set('views', './views');
/* istanbul ignore next */
if (process.env.NODE_ENV !== 'test') {
// Setup the loggin format, depending on running environment
app.use(morgan(process.env.NODE_ENV === 'development' ? 'dev' : 'combined'));
}
app.use(bodyParser.urlencoded({ extended: true }));
app.use(function (request, response, next) {
response.locals.year = 2020;
response.locals.season = 'Summer';
next();
});
// Add the static middleware, pointed at the ./public directory
// app.use(express.static('public'));
//render index pg
app.get('/', function (request, response) {
response.render('index');
});
//redirect to name pg
app.post('/', function (request, response) {
response.redirect('/' + request.body.name);
});
//generate list from tasks array
app.get('/tasks', function (request, response) {
response.render('tasksTemplate', { tasksData: tasksArray });
});
//push user submitted task to task array
app.post('/tasks', function (request, response) {
tasksArray.push(request.body.task);
response.render('tasksTemplate', { tasksData: tasksArray })
});
//body of name page
app.get('/tasks/:index', function (request, response) {
response.render('taskTemplate', { task: tasksArray[request.params.index] });
});
// allow other modules to use the server
module.exports = app;
extends ./layout
block bodyContents
h1 Tasks
p
form(action="tasks", method="POST")
input(type="text" name="task")
button(type="submit") submit
ul
each task, index in tasksData
li
a(href="/tasks/" + index) #{task}
extends ./layout
block bodyContents
h1= task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment