错误:如果原因不明,则意外

时间:2016-08-12 12:05:13

标签: coffeescript

我不知道为什么会发生这种情况,但这里是文件的代码:

express = require "express"
fs = require "fs"

router = express.Router()

module.exports = (config) ->

    fileRe = /^([a-zA-Z0-9-_]+)(\.(png|jpg|jpeg))?$/i

    router.get '/:file', (req, res) ->

        file = req.params.file

        if !file or !fileRe.test file
            return res.error(404).send "404 Not Found"

        fileInfo = fileRe.exec file

        if !fileInfo[3]
            # .png is default if extension is ommited
            # abc => abc.png, but abc.jpg will stay
            file += ".png"

        if fs.access config.destination + file, fs.F_OK, (err) ->
            if err
                res.status(500).send "An error occured"
            else
                if !fileInfo[3] or fileInfo[3] == "png"
                    ct = "image/png"
                else if fileInfo[3] == "jpg"
                    ct = "image/jpg"

                opts =
                    lastModified: false
                    headers:
                        "Content-Disposition": "inline; filename=\"#{file}\""
                        "Content-Type": ""

    return router

我收到以下错误

/home/kindlyfire/Webroot/uplimg-server/src/web/view.coffee:24:9: error: unexpected if
    if fs.access config.destination + file, fs.F_OK, (err) ->
    ^^

我看着那些空间,那里没问题。有人知道它可能是什么吗?

1 个答案:

答案 0 :(得分:1)

你写的不是有效的coffeescript。具体来说,错误指向的是行上的逗号。我会提供有关如何修复它的信息,但我甚至无法告诉你在这里想要完成什么。您 为编译器(更不用说读者)提供了一种方法,能够明确地告诉您在代码中需要哪些分区:

# fine
if foo then (a, b) -> c

# also fine
if foo
  (a, b) ->
    c

# ??
if foo (a, b) -> c

# ????
if foo a, b -> c

bug的责备。请注意,这是如何使最小化问题的再现的一个很好的例子。我非常高兴,高度建议你阅读一份coffeescript风格指南,并自行遵守它。 哪个一个非常重要,它的一致性很重要。不要只是将互联网中的东西随机复制粘贴到您的代码中,重新编写它以遵循与其余代码相同的样式。这样做通常会带来额外的好处,即实现你复制的代码片段的工作方式。