您的位置:首页 > Web前端 > Node.js

Node.js(安装包版)、Sublime text3安装与配置

2016-06-07 11:17 489 查看
1.Node.js(安装包版)版本/下载/安装

























开始菜单出现cmd图标/程序文件夹











测试安装是否成功







2.npm/node.js包管理器(模块管理器),Node Package Manager,从 http://npmjs.org 维护的public package registry下载(上传)模块包,以便开发和分享

检测软件是否集成/安装了npm模块





3.Node.js(安装包版)基本配置

在nodejs主目录下新建 node_global(全局模块存放位置)和node_cache(缓存)2个文件夹,通过cmd配置到Node.js配置文件 中

以便全局安装插件











设置环境变量/电脑(右键)->属性->高级系统设置->环境变量

目的/为了方便在cmd中执行可执行命令和搜索模块

安装后,默认“用户变量”下“PATH”添加了“C:\Users\sarsers\AppData\Roaming\ npm

在“系统变量”下新建“NODE_PATH”添加“D:\nodejs\node_global\node_modules”

在“用户变量”下编辑“PATH”添加“D:\nodejs”

在“系统变量”下默认的“Path”相应自动增加了“D:\nodejs”













4.Node.js(安装包版)基本测试

测试程序

基本测试,第一个经典“hello world!”程序



用Node.js撰写的HTTP Server版 hello world示例(粘贴时可能多出多余代码,不影响测试结果)

var http = require('http');

http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(8000);

console.log('Server running at ' target='_blank'>http://127.0.0.1:8000/');[/code] 
另一个简单的TCP服务器示例,监听(Listening)端口7000并输出 (echo)之前输入的消息

var net = require('net');

net.createServer(function (stream) {
stream.write('hello\r\n');

stream.on('end', function () {
stream.end('goodbye\r\n');
});

stream.pipe(stream);
}).listen(7000);


5.Sublime text3版本/下载/安装



























6.Package Control安装/Sublime text3包(插件)管理工具

安装代码 https://packagecontrol.io/installat

import urllib.request,os,hashlib; h = '2915d1851351e5ee549c20394736b442' + '8bc59f460fa1548d1514676163dafc88'; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); by = urllib.request.urlopen( 'http://packagecontrol.io/' + pf.replace(' ', '%20')).read(); dh = hashlib.sha256(by).hexdigest(); print('Error validating download (got %s instead of %s), please try manual install' % (dh, h)) if dh != h else open(os.path.join( ipp, pf), 'wb' ).write(by)


Sublime text 3,按
ctrl+~或者菜单View > Show Console
打开命令窗口,粘贴以上代码并回车即可。











7.Sublime text3 配置环境变量/Node.js 编译环境

环境变量

系统变量Path中添加subl.exe所在文件夹位置D:\Sublime Text 3,是为了能在cmd中执行
subl
命令









Node.js 编译环境

是为了在Sublime text3中调用Node.js运行.js程序

Nodejs.sublime-build

文件位置 C:\Users\sarsers\AppData\Roaming\Sublime Text 3\Packages\User

注意”encoding”与”windows”内容及写法,否则乱码和报错

{
"cmd": ["node", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.js",
"shell":true,
"encoding": "utf-8",
"windows":
{
"cmd": ["taskkill","/F","/IM","node.exe","&","node", "$file"]
},
"linux":
{
"cmd": ["killall node; node", "$file"]
}
}














Nodejs.sublime-settings

文件位置 C:\Users\sarsers\AppData\Roaming\Sublime Text 3\Packages\User

“node_command”: “D:\nodejs\node.exe”,

“npm_command”: “D:\nodejs\npm.cmd”,

{
// save before running commands
"save_first": true,
// if present, use this command instead of plain "node"
// e.g. "/usr/bin/node" or "C:\bin\node.exe"
"node_command": "D:\nodejs\node.exe",
// Same for NPM command
"npm_command": "D:\nodejs\npm.cmd",
// as 'NODE_PATH' environment variable for node runtime
"node_path": false,

"expert_mode": false,

"ouput_to_new_tab": false
}
















重启 Sublime text3之后,配置就完成了

8.Sublime text3 测试

测试代码

var http = require('http');
var os = require('os');

http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});
response.end('Hello World\n');
}).listen(3000);

console.log('Server running at ' target='_blank'>http://127.0.0.1:3000/');[/code] 
运行代码
ctrl+b






浏览器显示 http://127.0.0.1:3000 内容Hello World





cmd输入
node
进入node模式,输入
.exit
退出node模式











cmd复制/粘贴

标题栏->右键,即可看到相关功能和设置



环境变量

系统变量,对所有用户起作用/用户变量,只对当前用户起作用

一般说设置环境变量,默认是说设置系统变量

用户变量要随系统变量做相应更改

用户变量/PATH,当前用户调用可执行命令搜索路径

系统变量/NODE_PATH,搜索Node.js模块所在路径

系统变量/Path,所有用户调用可执行命令搜索路径

环境变量配置界面

电脑(右键)->属性->高级系统设置->环境变量



win+R
,运行
sysdm.cpl




ctrl+~
调出Sublime text3 console控制台

ctrl+b
运行.js程序
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息