Code Monkey home page Code Monkey logo

queues's Introduction

Cache, Proxies, Queues

Setup

  • Clone this repo, run npm install.
  • Install redis and run on localhost:6379

A simple web server

Use express to install a simple web server.

var server = app.listen(3000, function () {

  var host = server.address().address
  var port = server.address().port

  console.log('Example app listening at http://%s:%s', 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')
})

Redis

You will be using redis to build some simple infrastructure components, using the node-redis client.

var redis = require('redis')
var 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)});

An expiring cache

Create two routes, /get and /set.

When /set is visited, 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, fetch that key, and send value back to the client: res.send(value)

Recent visited sites

Create a new route, /recent, which will display the most recently visited sites.

There is already a global hook setup, which will allow you to see each site that is requested:

app.use(function(req, res, next) 
{
...

Use the lpush, ltrim, and lrange redis commands to store the most recent 5 sites visited, and return that to the client.

Cat picture uploads: queue

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" localhost:3000/upload

Have 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.

Proxy server

Bonus: How might you use redis and express to introduce a proxy server?

See rpoplpush

queues's People

Contributors

chrisparnin avatar

Watchers

James Cloos avatar Zhewei Hu avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.