Skip to content

Instantly share code, notes, and snippets.

@paulbjensen
Created December 30, 2010 11:14
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulbjensen/759682 to your computer and use it in GitHub Desktop.
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 = {};
@TrevorBurnham
Copy link

Under Node, you don't have to define a globals yourself; you can use the built-in global object. Once something is attached to global, it's automatically in the global scope (the same way that window.x is accessible as just x in a browser environment).

Just remember that in order to set a global, you have to write, say, global.articleList = []; then you can get it as articleList. But if you wanted to replace articleList entirely, you'd have to write global.articleList = whatever; writing articleList = whatever would create a new variable with local scope.

With that in mind, here's how I'd rewrite your code (and no more need for tasty.js):

# app.coffee
fs       = require 'fs'
express  = require 'express'
tasty    = require './tasty.js'
app      = express.createServer()
md       = require("node-markdown").Markdown

firstArticleData = ''
global.articleList = []
fs.readdir 'articles', (err, files) ->
  articleList[0..] = files # splice to copy
  fs.readFile 'articles/' + 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: 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: articleList } }

app.listen 3000

@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