# Create an express app with routes /, /products and /services # Create a route that captures the product id after /product and returns it in response # Use a regex to restrict the ID param to 3 letters followed by 3-5 numbers # Route a route using regex that matches the entire route # Create a simple check for correct product IDs. If it fails, show a custom error page # Use app.all() to check user permission before showing (mock) edit controls on a page express = require "express" app = express.createServer() app.use express.logger format: ":method :url" app.get "/", (req, res) -> res.write "Front page" res.end() canUserEdit = -> true app.all "/products/*", (req, res, next) -> if canUserEdit() res.write "" next() app.get "/products", (req, res) -> res.end "Our cool products" findProduct = (id) -> if id isnt "abc123" return false true app.get "/products/:id([a-z]{3}\\d{3})", (req, res, next) -> unless findProduct req.params.id return next() res.end "Product #{req.params.id}" app.get "/products/:id", (req, res) -> res.statusCode = 404 res.end "Product #{req.params.id} not found" app.get "/services", (req, res) -> res.end "Our even cooler services" app.get /// (.*) ///, (req, res, next) -> console.log "Got regex route #{req.url}, forwarding" next() app.listen 8003