- Clone this repo and change directory to the repo.
- Pull
queuesvirtual machine image which has the prerequisites you need for this workshop (nodejs, redis):bakerx pull CSC-DevOps/Images#Spring2020 queues - Create a new virtual machine using the
queuesimage:bakerx run queues queues --ip 192.168.44.81 --sync
- Run
bakerx ssh queuesto connect to the virtual machine. - Go to the sync folder (this repo) and install npm dependencies:
cd /bakerx npm install
In this workshop we use express to make a simple web server:
let server = app.listen(3000, function () {
const host = server.address().address;
const port = server.address().port;
console.log(`Example app listening at http://${host}:${port}`);
})Express uses the concept of routes to use pattern matching against requests and sending them to specific functions. You can simply write back a response body:
app.get('/', function(req, res) {
res.send('hello world')
})This functionality already exists in main.js.
You will be using redis server and node-redis client to build some simple infrastructure components:
const redis = require('redis');
const client = redis.createClient(6379, '127.0.0.1', {});In general, you can run all the redis commands in the following manner: client.CMD(args). For example:
client.set("key", "value");
client.get("key", function(err,value){ console.log(value)});Create two routes, /get and /set.
When /set is visited (i.e. GET request), set a new key, with the value:
"this message will self-destruct in 10 seconds".
Use the EXPIRE command to make sure this key will expire in 10 seconds.
When /get is visited (i.e. GET request), fetch that key, and send its value back to the client: res.send(value).
Create a new route, /recent, which will display the most recently visited sites.
There is already a global hook (middleware) setup, which will allow you to see each site that is requested:
app.use(function (req, res, next) {
...Use LPUSH, LTRIM, andLRANGE redis commands to store the most recent 5 sites visited, and return that to the client.
Implement two routes, /upload, and /meow.
A stub for upload and meow has already been provided.
Use curl to help you upload easily.
curl -F "image=@./img/morning.jpg" http://192.168.44.81:3000/uploadHave /upload store the images in a queue. Have /meow display the most recent image to the client and remove the image from the queue. Note, this is more like a stack and you can use LPOP redis command to implement this functionality.
Bonus: How might you use redis and express to introduce a proxy server?
See rpoplpush