node.js 第三方模块如何安装(使用npm)及介绍

写java代码,遇到问题首先去找相关的第三方APi,已经成为习惯了,java的第三方api真的是太多了。不过也不用羡慕java,node.js也不少,看看官网的介绍:

官方文档的最下面 Appendix 1: Recommended Third-party Modules ,这里是关于第三方模块的介绍,第一段:There are many third party modules for Node. At the time of writing, August 2010, the master repository of modules is the wiki page. 打开这个链接你会发现相当多的第三方模块,上百个是少不了的。

要安装使用这些模块需要使用npm(Node Package Manager)工具,该工具node.js已经集成,这个工具给我感觉像maven。打开命令行,执行npm -h(通常命令行应用程序都可以这样查看帮助,或者是/?,--help),结果如下:


发现有install和uninstall命令,这个就应该是安装和卸载的命令。根据说明以下方式可以查到具体帮助

npm install -h 结果如下:


这个帮助比较简单,具体的可以使用

npm help install 结果如下:




网页有该命令非常详细的介绍。uninstall也同样使用上述方法。


通常在网上看到安装是使用的这种方式 npm install connect,即 npm install <name>。那为什么只是给了name它就能安装,安装地址在哪啊?

install文档中提到了 a <name>@<version> that is published on the registry with (c)

那就看以下registry 的说明,文档下方SEE ALSO中有链接

registry文档中有下面一段话 To resolve packages by name and version, npm talks to a registry website 

that implements the CommonJS Package Registry specification for reading package info.

可以看出名称是由它来解析的,那地址在哪了,下面还有一句

The registry URL is supplied by the registry config parameter. See config(1) for more on managing npm's configuration.

我们在看一下 config(1) 链接,查看config文档,它就是一个全局设置,文档中有下面这段话:

registry

  • Default: https://registry.npmjs.org/
  • Type: url

The base URL of the npm package registry.

看到这个默认地址了吧,就是从这来的。这个就是官方地址:

The official public npm registry is at http://registry.npmjs.org/, It is powered by a CouchDB database at http://isaacs.iriscouch.com/registry.

基本上了解了。

在看一下安装到哪,cmd输入 npm help npm 查看npm文档,有这段描述:

See folders(1) to learn about where npm puts stuff.

In particular, npm has two modes of operation:

  • global mode:
    npm installs packages into the install prefix at prefix/lib/node_modules and bins are installed in prefix/bin.
  • local mode:
    npm installs packages into the current project directory, which defaults to the current working directory. Packages are installed to ./node_modules, and bins are installed to ./node_modules/.bin.

Local mode is the default. Use --global or -g on any command to operate in global mode instead.

意思很简单,我就不翻译了,点击folders(1)链接,查看folders文档,有以下内容:

  • Local install (default): puts stuff in ./node_modules of the current package root.
  • Global install (with -g): puts stuff in /usr/local or wherever node is installed.
  • Install it locally if you're going to require() it.
  • Install it globally if you're going to run it on the command line.
  • If you need both, then install it in both places, or use npm link我们通常使用require(),所以通常使用本地安装不使用全局安装。

    文档下方还有  prefix Configuration 、Node Modules 、Executables 三段介绍,看过以后可以有更具体的了解,我就不多说了。

    看express官方文档,可以证实:

    Installation

    $ npm install express
    

    or to access the express(1) executable install globally:

    $ npm install -g express
    到哪去找我需要的模块,有个网站必须提一下,http://search.npmjs.org/,这个就有点像maven的 http://mvnrepository.com/

    最近在研究以下几个第三方模块,分别介绍以下:

    1. 上传文件:node-formidable 官网 https://github.com/felixge/node-formidable,官网下方有相关介绍和API

    2.http中间件:connect(extjs同一家做了)  提供一个类似javaee过滤器的框架,提供了很多中间件,如:日志、静态文件服务器,seesion等功能,官网 https://github.com/senchalabs/connect/ ,使用或看源码推荐看 https://github.com/senchalabs/connect/tree/1.8.2,因为默认首页是2.0版,代码已经重写了,帮助文档 http://senchalabs.github.com/connect/,它内部上传文件功能使用的就是formidable

    3.web框架:express 提供了一个类似struts的框架,官网 http://expressjs.com/,文档 http://expressjs.com/guide.html,源码 https://github.com/visionmedia/express/,它是基于connect的。

    4.web socket:Socket.IO 官网 http://socket.io/,源码https://github.com/learnboost/socket.io/

    5.mongodb:node.js访问mongodb,官网https://github.com/christkv/node-mongodb-native

    先来formidable做个安装试验:

    cmd切换到工作目录,我的是D:\WebSite,

    输入npm list


    说明该目录没有安装任何东西

    在该工作目录下建名为upload.js的文件,内容如下:

    var formidable = require('formidable');
    输入node upload.js


    运行报错。

    开始安装输入npm install formidable,要等一会


    安装成功。该目录下会生成node_modules文件夹。

    现在可以使用了,再输入 node upload.js


    不报错了,证明安装成功可以使用。关于上传代码的实现,下次再讲。


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值