Skip to content

Instantly share code, notes, and snippets.

@jneira
Created August 25, 2011 20:07
Show Gist options
  • Save jneira/1171737 to your computer and use it in GitHub Desktop.
Save jneira/1171737 to your computer and use it in GitHub Desktop.
Clojurescript / node.js basic examples
(ns express_sample
(:require [cljs.nodejs :as node]))
(def express (node/require "express"))
(def app (. express (createServer)))
(defn -main [& args]
(doto app
(.use (. express (logger)))
(.get "/" (fn [req res]
(.send res "Hello World")))
(.listen 3000))
(println (str "Express server started on port: "
(.port (. app (address))))))
(set! *main-cli-fn* -main)
// from http://blog.nodeknockout.com
var express = require('express'),app = express.createServer()
app.use(express.logger())
app.get('/', function(req, res){
res.send('Hello World')
})
app.listen(3000)
console.log('Express server started on port %s', app.address().port)
(ns helloserver
(:require [cljs.nodejs :as nodejs]))
(def http (nodejs/require "http"))
(defn greetHandler [req res]
(doto res
(.writeHead 200 {"Content-Type" "text/plain"})
(.end "Hello World")))
(defn server [handler port url]
(-> (.createServer http handler)
(.listen port url)))
(defn -main [& mess]
(server greetHandler 1337 "127.0.0.1")
(println "Server running at http://127.0.0.1:1337/"))
(set! *main-cli-fn* -main)
var http = require('http')
function greetHandler (req,res) {
res.writeHead (200,{'Content-Type': 'text/plain'})
res.end('Hello World\n')
}
function server (handler,port,url) {
http.createServer(handler).listen(port,url)
}
server (greetHandler,1337, "127.0.0.1")
console.log('Server running at http://127.0.0.1:1337/')
@hmbadiwe
Copy link

Thanks for the examples.
I was curious. How would you get express-sample.cljs to work with current versions of express ( ">0.3.6") as createServer is no longer supported?

Copy link

ghost commented Dec 24, 2013

thanks :)

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