nodejs笔记



Windows平台下的node.js安装

直接去nodejs的官网http://nodejs.org/dist上下载nodejs安装程序node.exe

测试安装是否成功:

在命令行输入 node –v 应该可以查看到当前安装的nodejs版本号

或者在这里下载



简单的例子
写一段简短的代码,保存为helloworld.js,大致看下nodejs是怎么用的。

如下:该代码主要是创建一个http服务器




var http = require("http");

http.createServer(function(request, response) {

    response.writeHead(200, {"Content-Type": "text/html"});

    response.write("Hello World!");

    response.end();

}).listen(8080);

console.log("Server running at http://localhost:8080/");



打开命令行,转到当前文件所存放的路径下,运行 node helloworld.js命令即可

如果一切正常,可以看到命令行输出:Server running at http://localhost:8080/

同时,在浏览器输入http://localhost:8080/,可以看到一个写着helloworld的网页

设置NODE_PATH

NODE_PATH=D:\nodejs\node_global

安装npm

npm上有很多优秀的nodejs包,来解决常见的一些问题,比如用node-mysql,就可以方便通过nodejs链接到mysql,进行数据库的操作

在开发过程往往会需要用到其他的包,使用npm就可以下载这些包来供程序调用

有Git 可以使用git下载npm源码

git@github.com:npm/npm.git


git clone git@github.com:npm/npm.git

下载到NPM文件后,命令行首先转到npm所在地址,输入以下代码进行安装。


node cli.js install npm -gf

设置npm的cache和prefix:
npm config set prefix "D:\nodejs\node-global"
npm config set cache "D:\nodejs\node-cache"


查看哪些过期了:
D:\nodejs>npm outdated
Package  Current   Wanted  Latest  Locatio
npm       6.14.4  6.14.16   8.5.0  global

更新npm:
npm install npm@latest -g
windows下好像不行,我没有更新成功,只能更换了一个软件包重新解压缩安装。


npm设置代理地址: 
npm config set registry=http://registry.npmmirror.com/



npm init使用

D:\data\node-dev\npm-prj>npm init

This utility will walk you through creating a package.json file.

It only covers the most common items, and tries to guess sensible defaults.


See `npm help init` for definitive documentation on these fields

and exactly what they do.


Use `npm install <pkg>` afterwards to install a package and

save it as a dependency in the package.json file.


Press ^C at any time to quit.

package name: (npm-prj)

version: (1.0.0)

description:

entry point: (index.js)

test command:

git repository:

keywords:

author:

license: (ISC)

About to write to D:\data\node-dev\npm-prj\package.json:


{

  "name": "npm-prj",

  "version": "1.0.0",

  "description": "",

  "main": "index.js",

  "scripts": {

    "test": "echo \"Error: no test specified\" && exit 1"

  },

  "author": "",

  "license": "ISC"

}


npm设置cache和prefix: 

npm config set prefix "D:\nodejs\node-global"

npm config set cache "D:\nodejs\node-cache"


npm设置代理地址

npm config set registry=http://registry.npm.taobao.org


npm查看配置 npm config list

; cli configs

metrics-registry = "http://registry.npm.taobao.org/"

scope = ""

user-agent = "npm/6.14.16 node/v12.22.10 win32 x64"


; userconfig C:\Users\Administrator\.npmrc

cache = "D:\\nodejs\\node-cache"

prefix = "D:\\nodejs\\node-global"

registry = "http://registry.npm.taobao.org/"


; node bin location = D:\nodejs\node.exe

; cwd = D:\data\node-dev\npm-prj

; HOME = C:\Users\Administrator

; "npm config ls -l" to show all defaults.


npm 强制安装

npm install packageName --force 


npm查看帮助文档

例如: npm help install 

会在浏览器内打开这个页面: 
file:///D:/nodejs/node_modules/npm/docs/public/cli-commands/npm-install/index.html