trying to get coffeescript to compile correctly to javascript for node server
I'm working my way through a book on node.js, but I'm attempting to learn
it in coffeescript rather than javascript.
Currently attempting to get some coffeescript to compile to this js as
part of a routing demonstration:
var http = require('http'),
url = require('url');
http.createServer(function (req, res) {
var pathname = url.parse(req.url).pathname;
if (pathname === '/') {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('Home Page\n')
} else if (pathname === '/about') {
res.writeHead(200, {
'Content-Type': 'text/plain'
});
res.end('About Us\n')
} else if (pathname === '/redirect') {
res.writeHead(301, {
'Location': '/'
});
res.end();
} else {
res.writeHead(404, {
'Content-Type': 'text/plain'
});
res.end('Page not found\n')
}
}).listen(3000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
Here's my coffeescript code:
http = require 'http'
url = require 'url'
port = 3000
host = "127.0.0.1"
http.createServer (req, res) ->
pathname = url.parse(req.url).pathname
if pathname == '/'
res.writeHead 200, 'Content-Type': 'text/plain'
res.end 'Home Page\n'
else pathname == '/about'
res.writeHead 200, 'Content-Type': 'text/plain'
res.end 'About Us\n'
else pathname == '/redirect'
res.writeHead 301, 'Location': '/'
res.end()
else
res.writeHead 404, 'Content-Type': 'text/plain'
res.end 'Page not found\n'
.listen port, host
console.log "Server running at http://#{host}:#{port}/"
The error that I'm getting back is:
helloworld.coffee:14:1: error: unexpected INDENT
res.writeHead 200, 'Content-Type': 'text/plain'
^^^^^^^^
which makes me think that there's something wrong with the way that I have
the if...else logic set up.
Any thoughts why this might be happening?
No comments:
Post a Comment