Created
August 25, 2011 20:07
-
-
Save jneira/1171737 to your computer and use it in GitHub Desktop.
Clojurescript / node.js basic examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/') |
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?
thanks :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Lovely example, thanks!