环境准备
- 提前安装好node.js
- 提前安装好sublimeText 3
sublimeText安装Node.js插件
- 直接安装
在sublimeText上安装:command+Shift+P 调出Package Control 回车 => 搜索nodejs => 安装
- git安装
未实际操作过,需要的可以自行看官网说明https://packagecontrol.io/packages/Nodejs
修改配置文件
- 查看自己安装的node、npm位置
$ which node
/usr/local/Cellar/node/0.12.7/bin/node
$ which npm
/usr/local/Cellar/node/0.12.7/bin/npm
每个人安装的路径不一样,实际以查询到的路径为准。
- 进入插件目录
点击sublimeText 3 的Preferences -》Browse Packages 即可进到安装的插件目录
然后进入Nodejs目录
- 配置nodejs插件配置文件Nodejs.sublime-settings
修改里面的node_command
和 npm_command
配置项为实际的路径
{
// 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": "/usr/local/Cellar/node/0.12.7/bin/node",
// Same for NPM command
"npm_command": "/usr/local/Cellar/node/0.12.7/bin/npm",
// as 'NODE_PATH' environment variable for node runtime
"node_path": false,
"expert_mode": false,
"output_to_new_tab": false
}
- 配置nodejs插件配置文件Nodejs.sublime-build
修改里面的encoding
编码为utf8
{
"cmd": ["node", "$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
"selector": "source.js",
"shell": true,
"encoding": "utf8",
"windows":
{
"shell_cmd": "taskkill /F /IM node.exe & node $file"
},
"linux":
{
"shell_cmd": "killall node; /usr/bin/env node $file"
},
"osx":
{
"shell_cmd": "killall node; /usr/bin/env node $file"
}
}
测试
- 新建demo.js
var http = require('http');
http.createServer(function (request, response) {
// 发送 HTTP 头部
// HTTP 状态值: 200 : OK
// 内容类型: text/plain
response.writeHead(200, {'Content-Type': 'text/plain'});
// 发送响应数据 "Hello World"
response.end('Hello World\n');
}).listen(8888);
// 终端打印如下信息
console.log('Server running at http://127.0.0.1:8888/');
- command+B运行
sublimeText提示下面的信息,那就证明成功了。
Server running at http://127.0.0.1:8888/
在浏览器访问http://127.0.0.1:8888/,能显示Hello World
代表成功。