Tag: coffeescript

CoffeeScript – configurationSublimeText作为一个简单的IDE

我是CoffeeScript的新手,我想用SublimeText来试验它。 理想情况下,我想模仿小提琴沙拉优秀的在线编辑function – 特别是我想看到输出 – 即这样 console.log 'Hello World' 实际上在控制台中显示“Hello World”… 我在Windows上,并按照这些说明 ,并使用以下“生成”文件: { "cmd": ["coffee.cmd","-c","$file"], "file_regex": "^(…*?):([0-9]*):?([0-9]*)", "selector": "source.coffee" } 这让我尽可能编译咖啡文件到JavaScript,但没有进一步。 顺便说一句,我不禁想知道为什么这么难。 我热衷于学习(直到这一点),我不认为自己是一个白痴,但似乎Web应用程序的发展几乎故意深奥和过度。 帮助赞赏。

如何在Multer Express中validationfile upload的大小?

我使用Multer作为Express中的 multipart/form-data中间件。 我想知道如何validation上传的文件的大小,最好是他们正在上传。 我知道你可以在实例化Multer的时候在options对象中设置limits : app.use multer limits: fileSize: 1024 * 1024 但是,这只会截断上传的文件,不允许显示“文件太大”的错误消息。 当文件大小超过限制时。 我也检出了事件处理函数onFileUploadData(file, data) ,您可以在其中访问file对象和data缓冲区。 在这里,我可以通过检查data.length来检查当前的文件大小。 但是,我不清楚如何处理data.length大于我想允许的最大上传文件大小的情况。 最终,我的想法是,当Multerparsing请求并且上传的文件太大时,我想向用户显示一条Flash消息并redirect到表单,以便可以尝试更小的文件。 我的控制器的create行为看起来像这样: exports.create = (req, res) -> Record.create(req.body) .success (record) -> image = req.files.image uploadImage(image, record.id).then -> req.flash 'success', 'Record created.' res.redirect "/records/#{record.id}" .error (err) -> req.flash 'error', err res.redirect 'records/new' 问题是, req.files.image已经是parsing的图像,此时上传到系统tmp文件夹。 所以即使在这里检查文件的大小也不会真的让我防止不必要的大file upload。 一般来说,使用Multer或其他表单parsing中间件来处理Express的file […]

module.exports和外部范围

foo.coffee: class Foo constructor: -> console.log BAR module.exports = Foo main.coffee: BAR = 1 class Bar constructor: -> console.log BAR new Bar() Foo = require './foo' new Foo() 然后 $ coffee main.coffee 1 ReferenceError: BAR is not defined 为什么在Foo的实例中不能访问BAR ? 我可以使它对Foo对象“可见”(除了显式传递给构造函数外)吗?

Node.js,coffeescript和编译的js文件

我明白,可以原生使用CoffeeScript代替JavaScript,而不需要将JavaScript编译成CoffeeScript文件(至less不能将它们编译为文件;也许在内存中或者即时编译)。 我有一个主app.coffee文件的基本应用程序如下: coffee = require 'coffee-script' coffee = require 'coffee-script/register' express = require 'express' compression = require 'compression' sockets = require 'socket.io' http = require 'http' server = http.createServer express app = express() # […] 在我的package.json中,我有以下几点: // … "dependencies": { "coffee-script": "^1.9.3" // … } "scripts": { "start": "coffee app.coffee –nodejs" // … } // […]

这个方法如何调用工作?

我在PDFKit中使用PDFKit将一些图像添加到PDF文档。 其中一个例子是: # Fit the image within the dimensions doc.image('images/test.jpeg', 320, 15, fit: [100, 100]) .rect(320, 15, 100, 100) .stroke() .text('Fit', 320, 0) 我使用纯JS而不是coffeescript,我不明白第一行如何工作。 如何通过一个键值对作为参数传入,什么是JS等价物? 文档在这里: http : //pdfkit.org/docs/images.html

Javascript代理:节点代理,和谐,混合对象与魔法getter和setter …在Coffeescript?

我想有一个代理对象的方法和私有variables附加​​到它。 也就是说,所有正常的对象属性: foo = {} foo.bar = "baz" foo.boo = "hoo" 与一些原型: foo.setPrivateThings = function(value){ if (value) private = value; return private; } 枚举跳过私有variables/函数: console.log(foo); // { bar: "baz", boo: "hoo" } get / set将通过一个神奇的getter / setter来运行: foo.doesntexist = "…" = function(key){ console.log "Setting "+key; return new Date(); } 到目前为止,我有这个使用节点代理hokey Coffeescript。 任何更好的答案? class Data constructor: […]

如何从asynchronous调用中返回值

我在coffeescript中有以下function: newEdge: (fromVertexID, toVertexID) -> edgeID = this.NOID @client.methodCall('ubigraph.new_edge', [fromVertexID, toVertexID], (error, value) -> if(error) console.log('ubigraph.new_edge error: ' + error) edgeID = value ) edgeID 其中@ client.methodCall是指xmlrpc库。 我的问题是如何返回值为edgeID。 我是否使用callback? 如果是这样,callback应该是这样的:? # callback is passed the following parameters: # 1. error – an error, if one occurs # 2. edgeID – the value of the returned […]

为V8优化CoffeeScript生成的JS

下面的简单代码片段的coffee生成的JS: console.log 'b' if 'b' in arr 是 var __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; if (__indexOf.call(arr, 'b') >= 0) { console.log('b'); } 我能理解为什么是这样。 IE不支持indexOf ,我们希望确保我们的CS代码在所有浏览器上都能顺利运行​​。 但是,当为Node.js服务器编写代码时,我们确切知道JS引擎支持的是什么( ECMA-262,第5版 […]

在Node.js中处理Coffeescript代码

我如何处理一个咖啡脚本代码直接到JavaScript的node.js? 也许我可以使用咖啡编译器使用child_process.exec / spawn / fork但是有直接的方法吗? 像加载一个库(当它与npm一起安装时附带咖啡包)并将代码传递给它,直接返回编译的代码?

在express.js中的每个请求上设置全局res.localvariables

基本上我想要build立一个站点宽的导航栏,其中包含在express.js中的本地variables中指定的链接 所以我build立了我想要包含在页面上的链接列表: class NavigationLink constructor: (@title,@url) -> app.use (req,res,next) -> res.locals.navigationLinks = [ new NavigationLink "About", "/about" new NavigationLink "Projects", "/projects" new NavigationLink "Skills", "/skills" new NavigationLink "Contact", "/contact" ] next() 那么如果我打到主页: app.get '/', (req,res) -> res.render('about') Jade试图呈现这个布局文件,该文件应该循环遍历navigationLinks数组并为每一个渲染: !!! 5 html include header body .container .row .span3 ul.nav.nav-tabs.nav-stacked for navigationLink in navigationLinks include navigationLink .span9 […]