Tag: javascript

咕噜不能find模块'咖啡脚本'

我使用下面的命令来安装平均堆栈并创build一个应用程序: > sudo npm install -g meanio@latest // Get the mean cmdline > mean init myApp // create your first app > cd myApp && npm install // Install dependencies > grunt // Launch mean 但是当我到达最后的咕噜声命令时,我得到错误: module.js:333 throw err; ^ Error: Cannot find module 'coffee-script' at Function.Module._resolveFilename (module.js:331:15) at Function.Module._load (module.js:273:25) at Module.require (module.js:357:17) at […]

赶上expressParser错误

我想从bodyParser()中间件发送错误,当我发送一个JSON对象,它是无效的,因为我想发送一个自定义的响应,而不是一个通用的400错误。 这是我有,它的工作原理: app.use (express.bodyParser ()); app.use (function (error, req, res, next){ //Catch bodyParser error if (error.message === "invalid json"){ sendError (res, myCustomErrorMessage); }else{ next (); } }); 但是这在我看来是一个非常丑陋的方法,因为我正在比较可能在将来的快速版本中改变的错误消息。 还有其他方法可以捕获bodyParser()错误? 编辑: 这是请求主体有一个无效的json时的错误: { stack: 'Error: invalid json\n at Object.exports.error (<path>/node_modules/express/node_modules/connect/lib/utils.js:55:13)\n at IncomingMessage.<anonymous> (<path>/node_modules/express/node_modules/connect/lib/middleware/json.js:74:71)\n at IncomingMessage.EventEmitter.emit (events.js:92:17)\n at _stream_readable.js:872:14\n at process._tickDomainCallback (node.js:459:13)', arguments: undefined, type: undefined, message: […]

如何从另一个Node.js脚本中运行Node.js脚本

我有一个独立的Node脚本,名为compile.js 。 它坐在一个小型快递应用程序的主文件夹内。 有时我会从命令行运行compile.js脚本。 在其他情况下,我希望它由Express应用程序执行。 这两个脚本都从package.json加载configuration数据。 Compile.js目前不能导出任何方法。 加载这个文件并执行它的最好方法是什么? 我已经看了eval() , vm.RunInNewContext ,并require ,但不知道什么是正确的方法。 谢谢你的帮助!!

连锁的承诺没有通过拒绝

我有一个问题,理解为什么拒绝不通过承诺链传递,我希望有人能够帮助我理解为什么。 对我来说,将function附加到一系列的承诺意味着我的意图是我依靠一个原始的承诺来实现。 这很难解释,所以让我首先展示一个我的问题的代码示例。 (注意:这个例子是使用Node和延迟节点模块,我用Dojo 1.8.3testing过,结果相同) var d = require("deferred"); var d1 = d(); var promise1 = d1.promise.then( function(wins) { console.log('promise1 resolved'); return wins;}, function(err) { console.log('promise1 rejected'); return err;}); var promise2 = promise1.then( function(wins) { console.log('promise2 resolved'); return wins;}, function(err) { console.log('promise2 rejected'); return err;}); var promise3 = promise2.then( function(wins) { console.log('promise3 resolved'); return wins;}, […]

使用babel时意外的保留字'import'

在我的NodeJSv4.1.1代码中使用Babel。 得到了要求挂钩在: require("babel-core/register"); $appRoot = __dirname; module.exports = require("./lib/controllers/app"); 在随后的lodaded .js文件中,我正在做: import { Strategy as LocalStrategy } from "passport-local"; 但是,这在CLI中产生以下错误: import { Strategy as LocalStrategy } from "passport-local"; ^^^^^^ SyntaxError: Unexpected reserved word at exports.runInThisContext (vm.js:53:16) at Module._compile (module.js:413:25) at loader (/Users/*/Documents/Web/*/node_modules/babel-core/node_modules/babel-register/lib/node.js:128:5) at Object.require.extensions.(anonymous function) [as .js] (/Users/*/Documents/Web/*/node_modules/babel-core/node_modules/babel-register/lib/node.js:138:7) at Module.load (module.js:355:32) at Function.Module._load (module.js:310:12) at […]

恰当的方式来跳过Q Promise中的then函数

在我的代码中,基于特定的条件,我想跳过done函数,而不pipe所有的函数。 这个问题的原始版本是在编辑。 以下是我正在处理的实际问题。 抱歉给你带来不便 实际问题: 我在读文件并处理它。 如果文件的内容符合一定的条件,就必须对文件系统进行一系列的操作(比如读写几个文件),然后执行donefunction。 如果条件失败, 我必须跳过所有的一系列操作,我必须直接执行done函数 。 我在所有的函数中返回一个对象(让我们说result ), then在下一次我更新result并返回它。 那么,当所有这些都完成后,就会有累积的result 。 最后, done会处理result并打印出来。 所以,如果条件最初没有得到满足,那么只会打印result (这将是空的)。 Q() .then(readFile) .then(function (contents) { var processResult = process the contents; if (processResult) { return {}; } else { // How can I skip to `done` from here } }) .then(function (results) { // do some […]

将html表格导出到csv

我正在尝试在我的网站中添加csv下载选项的function。 它应该将网站中的html表格转换为csv内容并使其可下载。 我一直在通过互联网寻找一个好的插件,并find一些像http://www.dev-skills.com/export-html-table-to-csv-file/有用的,但它使用PHP脚本做下载部分。 我想知道是否有一个纯粹的JavaScript库可用于执行此function使用服务器端的软件如node.js而不使用PHP?

你如何使用npm脚本运行一个js文件?

我不能让npm工作。 我的package.json文件有 "scripts": { "build": "build.js" } 我有一个build.js文件在只是console.logs相同的文件夹。 当我跑步 npm run build 我得到错误 The system cannot execute the specified program. npm ERR! Windows_NT 6.1.7601 npm ERR! argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "build" npm ERR! node v4.1.1 npm ERR! npm v3.3.5 npm ERR! code ELIFECYCLE 如果我移动build.js文件并将我的package.json文件更改为具有子文件夹 "scripts": { "build": "build/build.js" } 那么我得到的错误 'build' is not […]

服务器端渲染与反应,反应路由器,并表示

我试图为我的反应应用程序设置服务器端渲染,我试图使用伟大的react-router模块来允许它处理非js的情况(一些爬虫,当用户closuresjs原因)。 但是,我遇到了麻烦。 我一直在使用伟大的响应在这里https://stackoverflow.com/a/28558545/3314701作为指导,但我得到了奇怪的错误抛给我。 尝试使用react.renderToString()时,我得到一个持久的Syntax Error 。 我是否设置了错误的服务器端渲染,丢失了明显的东西,还是其他的东西? 我的设置: 真正基本的快递服务器 require('babel/register'); var app = express(); // misc. express config… var Router = require('react-router'), routes = require('../jsx/app').routes, React = require('react'); app.use(function(req, res, next) { var router = Router.create({location: req.url, routes: routes}); router.run(function(Handler, state) { console.log(Handler); var html = React.renderToString(<Handler/>); return res.render('react_page', {html: html}); }); }); 顶级反应<App/>组件 // […]

JSHint:覆盖整个文件夹的单个.jshintrc选项

我有一个.jshintrc在我的项目的根目录下面的configuration: { "node": true, "smarttabs": true, "undef": true, "unused": true } 这对于我在项目中所有与节点相关的东西都是好的,但是对于浏览器相关的脚本来说并不是这样。 哪些坐在子文件夹中。 是否可以覆盖node选项,同时保留整个文件夹的其他选项? 如果我为浏览器端文件夹创build另一个.jshintrc文件,我必须再次告诉JSHint关于我所有的configuration,虽然我实际上只想取消设置node选项。 我知道我可以在每个文件中设置这个选项,但我实际上想避免这种情况。 提前谢谢了!