Created
June 13, 2018 00:27
-
-
Save alexciarlillo/abb35df65df38d2d83c7ed603c7cd0a7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extends ./layout | |
block bodyContents | |
h1= task |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment