Skip to content

Instantly share code, notes, and snippets.

@billiegoose
Last active August 29, 2015 14:09
Show Gist options
  • Save billiegoose/a5656b01c35d6f25dda7 to your computer and use it in GitHub Desktop.
Save billiegoose/a5656b01c35d6f25dda7 to your computer and use it in GitHub Desktop.
Simple Express Parameter Parsing Demo
# app.coffee
colors = require 'colors'
express = require 'express'
bodyParser = require 'body-parser'
port = 8080
app = express()
app.use bodyParser.urlencoded
extended: true
app.get '/test/:user/:homework/:stuff', (req, res) ->
res.type('.txt')
res.send """User: #{req.params.user}
Hw: #{req.params.homework}
Stuff: #{req.params.stuff}
Query: #{JSON.stringify(req.query)}"""
app.listen port
# If we were started by root drop permissions.
try
process.setgid('nogroup')
process.setuid('nobody')
console.log "Server running on port #{port}".green
console.log "Try visiting http://localhost/test/user/homework/stuff?key=value"

Simple Express Parameter Parsing Demo

  1. git clone https://gist.github.com/a5656b01c35d6f25dda7.git demo
  2. cd demo
  3. npm install
  4. npm start

Look at the purty code. See how the magic be done.

{
"name": "parse_demo_for_express",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Will Hilton",
"license": "MIT",
"dependencies": {
"body-parser": "^1.9.2",
"coffee-script": "^1.8.0",
"colors": "^1.0.3",
"express": "^4.10.1"
}
}
// server.js
// This is a bootstrap file to run app.coffee when you run `npm start`.
process.chdir(__dirname); // For safer use relative paths
require('coffee-script/register'); // Use CoffeeScript
require('./app'); // Actual server code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment