require

Handling Node.js Async Returns with “require” (Node ORM)

喜你入骨 提交于 2019-12-07 10:33:24
问题 I'm using the Node.js ORM module : https://github.com/dresende/node-orm I'm able to create a model by doing this: var orm = require("orm"); var db = orm.connect("creds", function (success, db) { if (!success) { console.log("Could not connect to database!"); return; } var Person = db.define("person", { "name" : { "type": "string" }, "surname": { "type": "string", "default": "" }, "age" : { "type": "int" } }); }); The problem is that I want to put Person (and all other models for that matter)

模块化Javascript编程基础

房东的猫 提交于 2019-12-07 10:10:36
(一) 随着网站逐渐变成" 互联网应用程序 ",嵌入网页的Javascript代码越来越庞大,越来越复杂。 网页越来越像桌面程序,需要一个团队分工协作、进度管理、单元测试等等......开发者不得不使用软件工程的方法,管理网页的业务逻辑。 Javascript模块化编程,已经成为一个迫切的需求。理想情况下,开发者只需要实现核心的业务逻辑,其他都可以加载别人已经写好的模块。 但是,Javascript不是一种模块化编程语言,它不支持" 类 "(class),更遑论"模块"(module)了。(正在制定中的 ECMAScript标准 第六版,将正式支持"类"和"模块",但还需要很长时间才能投入实用。) Javascript社区做了很多努力,在现有的运行环境中,实现"模块"的效果。本文总结了当前"Javascript模块化编程"的最佳实践,说明如何投入实用。虽然这不是初级教程,但是只要稍稍了解Javascript的基本语法,就能看懂。 一、原始写法 模块就是实现特定功能的一组方法。 只要把不同的函数(以及记录状态的变量)简单地放在一起,就算是一个模块。   function m1(){     //...   }   function m2(){     //...   } 上面的函数m1()和m2(),组成一个模块。使用的时候,直接调用就行了。 这种做法的缺点很明显:"污染"了全局变量

NodeJS require('./path/to/image/image.jpg') as base64

左心房为你撑大大i 提交于 2019-12-07 08:19:01
问题 Is there a way to tell require that if file name ends with .jpg then it should return base64 encoded version of it? var image = require('./logo.jpg'); console.log(image); // data:image/jpg;base64,/9j/4AAQSkZJRgABAgA... 回答1: I worry about the "why" , but here is "how" : var Module = require('module'); var fs = require('fs'); Module._extensions['.jpg'] = function(module, fn) { var base64 = fs.readFileSync(fn).toString('base64'); module._compile('module.exports="data:image/jpg;base64,' + base64

Using OpenLayers with RequireJS and AngularJS

瘦欲@ 提交于 2019-12-07 07:58:52
问题 I'm trying to get an app running that uses both AngularJS and RequireJS. I'm having problems getting my OpenLayers lib to work in this setup. I set the main AMD-modules in the main.js: require.config( { baseUrl: 'scripts', paths: { // Vendor modules angular: 'vendor/angular/angular', openLayers: 'vendor/openlayers-debug', other modules..... }, shim: { angular: { exports: 'angular' }, openLayers: { exports: 'OpenLayers' }, other modules.... } } ); require(['openLayers', 'angular', 'app',

Web scraping with CasperJS returns strange error that isn't documented

蹲街弑〆低调 提交于 2019-12-07 02:13:38
问题 I wrote an web scraping script with CasperJS and it works perfectly on Mac OS 10.10.4 with CasperJS version 1.1.0-beta3 and PhantomJS version 1.9.8, but when I put the same script on one of my servers which is Ubuntu 14.04 (running inside Docker container) with the same environment (CasperJS and PhantomJS all the same versions) it suddenly just outputs this: I'm `fs` modules Which is pretty strange. One of my suggestion is that in this script I am also trying to require some other scripts

当Shell遇上了NodeJS

青春壹個敷衍的年華 提交于 2019-12-07 01:37:46
序言 无论在传统的企业级系统维护还是在互联网运维中,Shell脚本的编写与维护常常必不可少,在系统管理员或开发人员工作中占比重比较大的一部分。 Shell脚本的严格语法格式对于一般的运维人员来说,常常会在一不留神下而抓狂或查找半天才发现是因为多了或少了一个空格或某语包括号不匹配而导致的错 误,不但大大的浪费了脚本维护人员的工作时间,还可能影响到工程进度甚至项目的发布里程碑等。当然,对于非纯Geek来说,最重要的还是影响心情,特别是 对于一些较复杂的脚本需求,更是必须小心谨慎,因此越来越多的开发人员必须借助于Python、Perl、Ruby等相关的脚本语言来实现,但是常由于平 台的特性或者语言的限制,对系统级的命令调用或者异常处理有限制,最终解决起来并不是十分优雅。 NodeJS的出现或许会给这些开发人员带来一些新的选择。 NodeJS 从诞生起发展非常迅速,社区活动非常活跃,目前扩展模块达到1500多个,并且每天都有不同的模块提交。它是构建在JavaScript引擎 V8 之上的JavaScript环境,它采用基于单线程的异步事件驱动I/O模型,具有非常高的 性能 , 同时能够支持多种平台。日前国外的很多大的软件或互联网公司如Microsoft,ebay,yahoo等都在使用NodeJS,国内的网易,淘宝,新浪 等互联网企业也有很多分享和成功的线上案例应用。言归正传

gulp构建前端工程

放肆的年华 提交于 2019-12-07 00:31:07
什么是gulp? gulp 是新一代的前端项目构建工具,你可以使用 gulp 及其插件对你的项目代码(less,sass)进行编译,还可以压缩你的 js 和 css 代码,甚至压缩你的图片, gulp 仅有少量的 API ,所以非常容易学习。 gulp 使用 stream 方式处理内容。 Node 催生了一批自动化工具,像 Bower,Yeoman,Grunt 等。 gulp和grunt的异同点 易于使用:采用代码优于配置策略,Gulp让简单的事情继续简单,复杂的任务变得可管理。 高效:通过利用Node.js强大的流,不需要往磁盘写中间文件,可以更快地完成构建。 高质量:Gulp严格的插件指导方针,确保插件简单并且按你期望的方式工作。 易于学习:通过把API降到最少,你能在很短的时间内学会Gulp。构建工作就像你设想的一样:是一系列流管道。 因为 gulp 是用 node.js 写的,所以你需要在你的终端上安装好 npm 。 npm 是 node.js 的包管理器,所以先在你的机子上安装好 node.js 吧 gulp 安装命令 sudo npm install -g gulp 在根目录下新建 package.json 文件 npm init . 安装 gulp 包 npm install gulp --save-dev 安装好后再次输入 gulp -v 查看版本号

富文本编辑器 froala-editor

痴心易碎 提交于 2019-12-06 23:25:12
https://blog.csdn.net/hjy170314/article/details/102696538 文本编辑器 froala-editor 官网文档: https://www.froala.com/wysiwyg-editor/examples/full-featured 使用方式: 安装 froala-eidtor cnpm install vue-froala-wysiwyg@2.9.0 --save cnpm install jquery --save 在mian.js中写入 import Vue from 'vue' import App from './App.vue' import './registerServiceWorker' import router from './router' import store from './store' // 富文本 start import jQuery from 'jquery' import VueFroala from 'vue-froala-wysiwyg' require('froala-editor/js/froala_editor.pkgd.min') require('froala-editor/css/froala_editor.pkgd.min.css') require('font

node.js一

风格不统一 提交于 2019-12-06 23:08:31
node.js创建第一个应用,本地服务器 创建 node 文档夹,新建 server.js 文档 1 node/server.js required 引入 http模块 12 //server.jsvar http = require("http"); 创建服务器 12345678910111213141516 //server.jsvar http = require('http');http.createServer(function (request, response) { // 发送 HTTP 头部 // 大专栏 node.js一HTTP 状态值: 200 : OK // 内容类型: text/plain response.writeHead(200, {'Content-Type': 'text/plain'}); // 发送响应数据 "Hello World" response.end('Hello Worldn');}).listen(8888);// 终端打印如下信息console.log('Server running at http://127.0.0.1:8888/'); 执行命令,显示效果 命令提示符 1 node server.js 打开浏览器访问 http://127.0.0.1:8888/,看到一个写着 “Hello World”的网页 分析node

gulp 使用初解

喜你入骨 提交于 2019-12-06 22:47:17
gulp gulp是基于Node.js的前端自动化构建工具,主要用于代码打包。目前主流的前端打包工具,grunt,gulp,webpack。grunt现在似乎慢慢被gulp取代,而我使用gulp也是冲着大家说的配置简单去的,至于通道流,运行文件什么的其实还是次要的;webpack现在很火,听说功能强大,没有用过。 npm install gulp -g 注意需要在npm全局中安装gulp,否则命令窗口中找不到gulp快捷命令,记得检查环境变量的配置情况。 gulp 插件 gulp的使用离不开gulp插件的使用,下面是常用的几款。 npm install del gulp gulp-concat gulp-minify-css gulp-usemin gulp-replace gulp-uglify gulp-rev gulp-rev-collector gulp-minify-html --save-dev del 和 gulp-clean del是node.js的插件,两者的效果相似,都是用来清除文件。至于为什么使用 del,而不是 gulp-clean,我是看到网上一篇文章推荐的。 gulp-concat gulp-concat用来做文件合并,可以将多个文件合并成一个文件,减少静态资源数量。 gulp-minify-css gulp-minify-css是css的压缩工具。