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上有很多优秀的nodejs包,来解决常见的一些问题,比如用node-mysql,就可以方便通过nodejs链接到mysql,进行数据库的操作
在开发过程往往会需要用到其他的包,使用npm就可以下载这些包来供程序调用
有Git 可以使用git下载npm源码
git@github.com:npm/npm.git
git clone git@github.com:npm/npm.git |
node cli.js install npm -gf |
npm config set prefix "D:\nodejs\node-global"
npm config set cache "D:\nodejs\node-cache"
D:\nodejs>npm outdatedPackage Current Wanted Latest Locationpm 6.14.4 6.14.16 8.5.0 global
npm设置代理地址:
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
Node.js基础 http://www.infoq.com/cn/master-nodejs
Node.js 入门教程 http://nodebeginner.org/index-zh-cn.html
Node.js中文文档 http://cnodejs.org/cman/index.html
Express文档 http://expressjs.com/guide.html#routing
CNode社区 http://club.cnodejs.org/