Info 253A: Frontend Web Architecture
Kay Ashaolu
app.js
var express = require('express'); // Adding the express library
var app = express(); // Initializing application
app.get('/', function (req, res) {
res.send("<html><head><title>Homepage</title></head>\
<body><h1>This is the home page!</h1></body></html>");
});
app.get('/about', function(req,res) {
res.send("<html><head><title>About</title></head>\
<body><h1>This is the about us page!</h1></body></html>")
});
app.get('/blog/post-1', function(req,res) {
res.send("<html><head><title>Blog 1</title></head>\
<body><h1>This is the first blog post</h1></body></html>")
});
var server = app.listen(3000, function () {
console.log('Server on localhost listening on port 3000');
});
app.js
var express = require('express'); // Adding the express library
var app = express(); // Initializing application
// Set the path "/static" to serve files from the static folder
app.use('/static', express.static(__dirname + '/static'));
// Define your routes here
app.get('/', function (req, res) {
res.send("<html><head><title>Homepage</title>\
<link rel='stylesheet' href='/static/css/style.css' /></head>\
<body><h1>This is the home page!</h1>\
<script type='text/javascript' src='/static/js/script.js'></script></body></html>");
});
// Start up server on port 3000 on host localhost
var server = app.listen(3000, function () {
console.log('Server on localhost listening on port 3000');
});
static/css/style.css
h1 {
color: blue;
}
static/js/script.js
alert("script.js has been loaded!");
npm install mustache-express --save
app.js
var express = require('express'); // Adding the express library
var mustacheExpress = require('mustache-express'); // Adding mustache template system
var app = express(); // initializing application
var template_engine = mustacheExpress(); // initializing template engine
// set the path "/static" to serve files from the static folder
app.use('/static', express.static(__dirname + '/static'));
// Set templating engine and location of templates
app.engine('html', template_engine); // set html parsing to the template engine
app.set('views', __dirname + '/views');
// Define your routes here
app.get('/', function (req, res) {
res.render('index.html');
});
// Start up server on port 3000 on host localhost
var server = app.listen(3000, function () {
console.log('Server on localhost listening on port 3000');
});
views/index.html
<html>
<head>
<title>Homepage</title>
<link rel='stylesheet' href='/static/css/style.css' />
</head>
<body>
<h1>This is the home page!</h1>
<script type='text/javascript' src='/static/js/script.js'></script>
</body>
</html>
app.js (1/3)
var express = require('express'); // Adding the express library
var mustacheExpress = require('mustache-express'); // Adding mustache template system
var app = express(); // initializing application
var template_engine = mustacheExpress(); // initializing template engine
// set the path "/static" to serve files from the static folder
app.use('/static', express.static(__dirname + '/static'));
// Set template engine and location of templates
app.engine('html', template_engine); // set html parsing to the template engine
app.set('views', __dirname + '/views');
app.js (2/3)
// An object that contains a quote that we want to display for a day of the week
var quote_db = {
'sunday': "Life is about making an impact, not making an income. \
–Kevin Kruse",
'monday': "Whatever the mind of man can conceive and believe, it can achieve. \
–Napoleon Hill",
'tuesday': "Strive not to be a success, but rather to be of value. \
–Albert Einstein",
'wednesday': "You miss 100% of the shots you don’t take. \
–Wayne Gretzky",
'thursday': "Every strike brings me closer to the next home run. \
–Babe Ruth",
'friday': "We become what we think about. \
–Earl Nightingale",
'saturday': "Life is what happens to you while you’re busy making other plans. \
–John Lennon",
}
app.js (3/3)
// Define your routes here
app.get('/', function (req, res) {
day = req.query.day_of_week;
console.log("Received request for quote for day " + day);
res.render('index.html', { 'day': day, 'quote': quote_db[day] } );
});
// Start up server on port 3000 on host localhost
var server = app.listen(3000, function () {
console.log('Server on localhost listening on port 3000');
});
views/index.html
<html>
<head>
<title>Homepage</title>
<link rel='stylesheet' href='/static/css/style.css' />
</head>
<body>
<h1>This is the home page!</h1>
<p>The quote for {{day}} is: {{quote}}</p>
<br />
<form action="/" method="GET">
<label for="day_select">Select a day</label>
<select name="day_of_week" id="day_select">
<option value="sunday">Sunday</option>
<option value="monday">Monday</option>
<option value="tuesday">Tuesday</option>
<option value="wednesday">Wednesday</option>
<option value="thursday">Thursday</option>
<option value="friday">Friday</option>
<option value="saturday">Saturday</option>
</select>
<input type="submit" value="Give me a quote" />
</form>
</body>
</html>