手机版
你好,游客 登录 注册
背景:
阅读新闻

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

[日期:2012-02-06] 来源:Linux社区  作者:dead-horse [字体:大 中 小]

我们再看一下 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的文件,内容如下:

    [javascript]
    1. var formidable = require('formidable');  
    输入node upload.js


    运行报错。

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


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

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


    不报错了,证明安装成功可以使用。关于上传代码的实现。参考 http://www.linuxidc.com/Linux/2012-02/53532.htm 与 http://www.linuxidc.com/Linux/2012-02/53530.htm

  • linux
    本文评论   查看全部评论 (0)
    表情: 表情 姓名: 字数

           

    评论声明
    • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
    • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
    • 本站管理人员有权保留或删除其管辖留言中的任意内容
    • 本站有权在网站内转载或引用您的评论
    • 参与本评论即表明您已经阅读并接受上述条款