[Node.js] Broswerify -- 1

Browserify is a tool that brings node.js style development to the browser.

The thing you can see on the node.js cmd line can also be broung up to the broswer.

What you need to do is:

Install the browserify:

npm install -g browserify

Now, for example, you have two javascript file:

entry.js:

/**
 * Created by Answer1215 on 12/10/2014.
 */
var test = require(‘./test‘);

console.log(test);

test.js:

/**
 * Created by Answer1215 on 12/10/2014.
 */
module.exports = [‘foo‘, ‘bar‘, ‘tool‘];

When you run in the commond line: npm entry.js

you will get the result:

C:\Users\Answer1215\WebstormProjects\angular\commonJSBroswerfiy>node entry
[ ‘foo‘, ‘bar‘, ‘tool‘ ]

But if you run browserfiy:

browserfiy entry.js -o bundle.js

It will create a new file which called bundle.js which minfiy the code, it looks like:

(function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module ‘"+o+"‘");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
/**
 * Created by Answer1215 on 12/10/2014.
 */
var test = require(‘./test‘);

console.log(test);
},{"./test":2}],2:[function(require,module,exports){
/**
 * Created by Answer1215 on 12/10/2014.
 */
module.exports = [‘foo‘, ‘bar‘, ‘tool‘];
},{}]},{},[1]);

Then you can create an html file whcih inclide the bundle.js:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<script src="bundle.js"></script>
</body>
</html>

Check in the broswer:

时间: 2024-08-25 11:46:22

[Node.js] Broswerify -- 1的相关文章

[Node.js] Broswerify -- 2

Browserify allows you to leverage 10s of thousands of javascript modules available in the Node Package Manager (npm) in your browser apps. Notice: // When there is ../ or ./, it will find the file accordingly var test = require('./test'); // If there

node.js的安装及配置

一.安装 直接在浏览器搜索node.js,在官网上下载(一般旧版的更加稳定,比如下载4.4.7版本) 点击DOWNLOADS 往下翻,点击Previous Release Windows下载msi(64位/32位) 根据提示一步步安装,安装之后的文件夹如下: 在cmd命令行下输入node -v,如果出现如下,说明安装成功: 二.关于配置 在安装路径下新建两个文件夹: 创建完两个空文件夹之后,打开cmd命令窗口,输入 npm config set prefix "D:\Program Files

Node.js: Extend and Maintain Applications + large scale

https://blog.risingstack.com/node-js-mysql-example-handling-hundred-gigabytes-of-data/ My secondary goal with this article is to help you decide if Node.js + MySQL is a good fit for your needs, and to provide help with implementing such a solution. h

1.node.js windows环境搭建

作为服务端运行javascript的平台的NodeJs,把前台javascript移到了服务器端,Google V8引擎使其运行效率非常高,它可以异步,无任何阻塞运行程序.nodejs包含http服务器,可以为我们实现 web系统设计,客户端javascript编译器,等一系列的功能. 工具/原料 windows系统电脑 ,电脑可以上网 方法/步骤 下载windows平台nodejs环境安装包,百度一下nodejs官网,找到DOWNLOADS点击,找到Windows Installer 如果为6

在Node.js中使用RabbitMQ系列二 任务队列

在上一篇文章在Node.js中使用RabbitMQ系列一 Hello world我有使用一个任务队列,不过当时的场景是将消息发送给一个消费者,本篇文章我将讨论有多个消费者的场景. 其实,任务队列最核心解决的问题是避免立即处理那些耗时的任务,也就是避免请求-响应的这种同步模式.取而代之的是我们通过调度算法,让这些耗时的任务之后再执行,也就是采用异步的模式.我们需要将一条消息封装成一个任务,并且将它添加到任务队列里面.后台会运行多个工作进程(worker process),通过调度算法,将队列里的任

node.js搭建代理服务器请求数据

1.引入node.js中的模块 1 var http = require("http"); 2 var url = require("url"); 3 var qs = require("querystring"); 2.创建服务器 //用node中的http创建服务器 并传入两个形参 http.createServer(function(req , res) { //设置请求头 允许所有域名访问 解决跨域 res.setHeader("

Node.JS 文件读写,把Sheet图集转换为龙骨动画图集

Node.JS 文件读写,把Sheet图集数据转换为龙骨动画图集数据 var fs = require("fs") var readline = require("readline"); var rl = readline.createInterface({ input:process.stdin, output:process.stdout }); var path = undefined; var dbName = undefined; rl.question(

10个常见的Node.js面试题

如果你希望找一份有关Node.js的工作,但又不知道从哪里入手评测自己对Node.js的掌握程度. 本文就为你罗列了10个常见的Node.js面试题,分别考察了Node.js编程相关的几个主要方面. 在进入正文之前,需要提前声明两点: 这些问题只是Node.js知识体系的一个局部,并不能完全考察被面试者的实际开发能力. 对现实世界开发中遇到的问题,需要的是随机应变与团队合作,所以你可以尝试结对编程. Node.js面试题列表 什么是错误优先的回调函数? 如何避免回调地狱? 如何用Node来监听8

CentOS6.5 安装Node.js

Node.js的安装通常有两种方式:自己编译源代码和使用编译好的文件,我这里使用编译好的文件目前我的home目录下有刚下载来的node-v4.2.3-linux-x641.首先解压缩 tar xvf node-v4.2.3-linux-x64 2.设置链接 ln -s /home/node-v4.2.3-linux-x64/bin/node /usr/local/bin/node ln -s /home/node-v4.2.3-linux-x64/bin/npm /usr/local/bin/n