Skip to content

Instantly share code, notes, and snippets.

@paulbjensen
Created December 30, 2010 11:14
Show Gist options
  • Select an option

  • Save paulbjensen/759682 to your computer and use it in GitHub Desktop.

Select an option

Save paulbjensen/759682 to your computer and use it in GitHub Desktop.
How I do global variables with CoffeeScript
# app.coffee
fs = require 'fs'
express = require 'express'
tasty = require './tasty.js'
app = express.createServer()
md = require("node-markdown").Markdown
firstArticleData = ''
articleList = []
fs.readdir 'articles', (err, files) ->
globals.articleList = files
fs.readFile 'articles/' + globals.articleList[0], (err,data) -> firstArticleData = md(new String(data))
app.configure ->
app.use express.staticProvider(__dirname + '/public')
app.set 'view engine', 'jade'
app.get '/', (req,res) ->
res.render 'index', { locals: { article: firstArticleData, list: globals.articleList } }
app.get '/:year/:month/:day/:title', (req,res) ->
articleUrl = [req.params.year, req.params.month, req.params.day, req.params.title].join('-')
fs.readFile 'articles/' + articleUrl + '.markdown', (err,data) ->
res.render 'index', { locals: { article: md(new String(data)), list: globals.articleList } }
app.listen 3000
// tasty.js, where I define a global variables holder
globals = {};
@paulbjensen
Copy link
Author

It works, thanks!

@curable-online
Copy link

@TrevorBurnham
Thanks as 2023 it's used by me to learn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment