Skip to content

Instantly share code, notes, and snippets.

@mayfer
Created July 15, 2015 17:32
Show Gist options
  • Save mayfer/d61d57098f2578e51e23 to your computer and use it in GitHub Desktop.
Save mayfer/d61d57098f2578e51e23 to your computer and use it in GitHub Desktop.
Caching
<!doctype html>
<html>
<head>
<title>weather</title>
<style>
body, html {
background: #05a;
color: #fff;
font-family: monospace;
}
</style>
</head>
<body>
<% for(var i = 0; i < list.length; i++ ) { %>
<%= list[i].dt_txt %><br />
<%= list[i].weather[0].main %><br /><hr />
<% } %>
</body>
</html>
var express = require('express');
var app = express();
var http = require('http').Server(app);
var path = require('path');
var redis = require("redis");
var client = redis.createClient();
var request = require('./request');
// tell express to use EJS templating
app.set('view engine', 'ejs');
app.get('/', function(req, res){
// set some data variables to use within the template
client.get("html", function(err, reply) {
if(err || reply == null) {
request.fetch("api.openweathermap.org", "/data/2.5/forecast?q=Vancouver,ca&mode=json", function(response) {
console.log("Fetched weather data from external site.");
var data = JSON.parse(response);
res.render(path.join(__dirname, 'caching.ejs'), data, function(err, html) {
client.set("html", data);
client.expire("html", 10);
last_fetch = Date.now();
res.send(html);
});
});
} else {
res.send(reply);
}
});
});
http.listen(8888, function(){
console.log('listening on *:8888');
});
app.use(express.static(__dirname));
rwx rwx rwx
111 111 111
rwx r-x r--
111 101 100
100 = ?
|
2^0 = 1
|
2^1 = 2
|
2^2 = 4
100 in binary = 5 in decimal
rwx r-x r--
111 101 100
7 5 4
chmod 754 filename.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment