Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在尝试将coffeescript正确编译为节点服务器的javascript_Javascript_Node.js_Coffeescript - Fatal编程技术网

正在尝试将coffeescript正确编译为节点服务器的javascript

正在尝试将coffeescript正确编译为节点服务器的javascript,javascript,node.js,coffeescript,Javascript,Node.js,Coffeescript,我正在阅读一本关于node.js的书,但我试图用coffeescript而不是javascript来学习它 目前正在尝试获取一些coffeescript以编译到此js,作为路由演示的一部分: var http = require('http'), url = require('url'); http.createServer(function (req, res) { var pathname = url.parse(req.url).pathname; if

我正在阅读一本关于node.js的书,但我试图用coffeescript而不是javascript来学习它

目前正在尝试获取一些coffeescript以编译到此js,作为路由演示的一部分:

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/'); 
以下是我的咖啡脚本代码:

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}/"
我得到的错误是:

helloworld.coffee:14:1: error: unexpected INDENT
        res.writeHead 200, 'Content-Type': 'text/plain'
^^^^^^^^
这让我觉得我设置
if…else
逻辑的方式有问题;当我编译时,它看起来也像是试图
返回
res.end
语句,而不是将其作为第二个要运行的函数添加


有没有想过为什么会发生这种情况,以及如何修复我的代码?

这纯粹是制表符与空格的问题。 确保编辑器不会将空格转换为制表符。另外,用光标浏览代码,确保它不会跳过空白区域。
问题是,虽然普通编辑器将选项卡视为相当于两个或四个空格,但coffeescript将其视为一个空格,因此缩进会变得混乱。

这纯粹是选项卡与空格的问题。 确保编辑器不会将空格转换为制表符。另外,用光标浏览代码,确保它不会跳过空白区域。
问题是,虽然普通的编辑器将选项卡视为相当于两个或四个空格,但coffeescript将其视为一个空格,因此缩进会变得混乱。

如果
的其他部分
更改为
的其他部分,除非最后一个部分是
res.writeHead 200,“内容类型”:“text/plain”
因为您在第一个else之后已经有了表达式-
else pathname=='/about'

将您的
else
更改为
else,如果除了最后一个,它在
res.writeHead 200上投诉,“内容类型”:“text/plain”
因为在第一个else之后已经有了表达式-
else pathname=='/about'