参考:
深入浅出Node.js(二):Node.js&NPM的安装与配置
去下载并安装Node.js。
1.去Node.js主页:
下载了最新版本的Node.js:
http://nodejs.org/dist/v0.8.14/x64/node-v0.8.14-x64.msi
并安装。
(后记:
其实所有的安装文件,都可以从:
中找到。)
然后把如下内容:
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(1337, "127.0.0.1");
console.log('Server running at http://127.0.0.1:1337/'); 保存为example.js,然后去cmd下运行:
D:\tmp\tmp_dev_root\node.js>node example.js Server running at http://127.0.0.1:1337/
然后去浏览器中打开:
验证是OK的,可以看到输出:
Hello World
2.本来打算参考教程,去:
http://code.google.com/p/msysgit/downloads/list
下载最新版的msysgit的,但是后来发现cygwin中已有git了,所以放弃安装,而使用cygwin中的git。
去在cygwin下,使用git去安装npm,中间出错但解决了:
3.然后可以看到npm中的内容了:
CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/node.js/npm/npm $ ls -l total 58 -rw-r--r--+ 1 CLi Domänen-Benutzer 3857 Nov 9 10:42 AUTHORS drwxr-xr-x+ 1 CLi Domänen-Benutzer 0 Nov 9 10:42 bin -rwxr-xr-x+ 1 CLi Domänen-Benutzer 48 Nov 9 10:42 cli.js -rwxr-xr-x+ 1 CLi Domänen-Benutzer 521 Nov 9 10:42 configure drwxr-xr-x+ 1 CLi Domänen-Benutzer 0 Nov 9 10:42 doc drwxr-xr-x+ 1 CLi Domänen-Benutzer 0 Nov 9 10:42 html drwxr-xr-x+ 1 CLi Domänen-Benutzer 0 Nov 9 10:42 lib -rw-r--r--+ 1 CLi Domänen-Benutzer 2769 Nov 9 10:42 LICENSE -rw-r--r--+ 1 CLi Domänen-Benutzer 3762 Nov 9 10:42 Makefile drwxr-xr-x+ 1 CLi Domänen-Benutzer 0 Nov 9 10:42 node_modules -rw-r--r--+ 1 CLi Domänen-Benutzer 2694 Nov 9 10:42 package.json -rw-r--r--+ 1 CLi Domänen-Benutzer 7669 Nov 9 10:42 README.md drwxr-xr-x+ 1 CLi Domänen-Benutzer 0 Nov 9 10:42 scripts drwxr-xr-x+ 1 CLi Domänen-Benutzer 0 Nov 9 10:42 test
然后接着去安装:
CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/node.js/npm/npm $ node cli.js install npm -gf npm http GET https://registry.npmjs.org/npm npm http 200 https://registry.npmjs.org/npm npm http GET https://registry.npmjs.org/npm/-/npm-1.1.65.tgz npm http 200 https://registry.npmjs.org/npm/-/npm-1.1.65.tgz C:\Program Files\nodejs\npm -> C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js npm@1.1.65 C:\Program Files\nodejs\node_modules\npm
安装完毕了。
4.继续通过npm去安装underscore:
CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/node.js/npm/npm
$ npm install underscore
cygwin warning:
MS-DOS style path detected: C:\Program Files\nodejs/node
Preferred POSIX equivalent is: /cygdrive/c/Program Files/nodejs/node
CYGWIN environment variable option "nodosfilewarning" turns off this warning.
Consult the user's guide for more details about POSIX paths:
http://cygwin.com/cygwin-ug-net/using.html#using-pathnames
npm http GET https://registry.npmjs.org/underscore
npm http 200 https://registry.npmjs.org/underscore
npm http GET https://registry.npmjs.org/underscore/-/underscore-1.4.2.tgz
npm http 200 https://registry.npmjs.org/underscore/-/underscore-1.4.2.tgz
underscore@1.4.2 node_modules\underscore至此,安装OK。
5.继续参考:
去试试模块.
把:
var PI = Math.PI;
exports.area = function (r) {
return PI * r * r;
};
exports.circumference = function (r) {
return 2 * PI * r;
};存为circle.js.
把:
var circle = require('./circle.js');
console.log( 'The area of a circle of radius 4 is ' + circle.area(4));存为app.js.
然后在cygwin中测试,结果路径不对:
CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/node.js
$ node app.js
module.js:340
throw err;
^
Error: Cannot find module './circle.js'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at Object.<anonymous> (D:\tmp\tmp_dev_root\node.js\app.js:1:76)
at Module._compile (module.js:449:26)
at Object.Module._extensions..js (module.js:467:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Module.runMain (module.js:492:10)修改了相对路径后:
var circle = require('./modules/circle/circle.js');
console.log( 'The area of a circle of radius 4 is ' + circle.area(4));即可测试成功:
CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/node.js $ node app.js The area of a circle of radius 4 is 50.26548245743669
6.后来,继续去参考另外一个教程:
去把
var http = require("http");
http.createServer(function(request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}).listen(8888);存为:server.js
然后cmd中运行:
D:\tmp\tmp_dev_root\node.js>node server.js
其中win7中还会看到跳出对话框问是否运行v8引擎访问网络,当然选择是了。
然后去访问:
的确可以看到输出:
Hello World
7.然后用contrl+C,终止了server.js的运行。
然后把代码改为:
var http = require("http");
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");然后重新运行,然后去浏览器中刷新几次,结果的输出如下:
D:\tmp\tmp_dev_root\node.js>node server.js Server has started. Request received. Request received. Request received.
可以看到,每次浏览器访问:
就打印了一次Request received.
不过也顺便要知道favicon.ico:
(请注意,当我们在服务器访问网页时,我们的服务器可能会输出两次“Request received.”。那是因为大部分服务器都会在你访问 http://localhost:8888 /时尝试读取 http://localhost:8888/favicon.ico )
8.同样的,把:
var server = require("./server");
server.start();存为index.js,然后把:
var http = require("http");
function start() {
function onRequest(request, response) {
console.log("Request received.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;存为server.js,然后去运行:
D:\tmp\tmp_dev_root\node.js>node index.js Server has started.
9.然后再去把:
function route(pathname) {
console.log("About to route a request for " + pathname);
}
exports.route = route;存为router.js;
把index.js改为:
var server = require("./server");
var router = require("./router");
server.start(router.route);把server.js改为:
var http = require("http");
var url = require("url");
function start(route) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(pathname);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;然后关闭旧server,重新启动。
然后浏览器中访问:
输出为:
D:\tmp\tmp_dev_root\node.js>node index.js Server has started. Request for /read received. About to route a request for /read Request for /write received. About to route a request for /write
10.然后依次地把:
server.js改为:
var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
route(handle, pathname);
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;把index.js改为:
var server = require("./server");
var router = require("./router");
var requestHandlers = require("./requestHandlers");
var handle = {}
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
server.start(router.route, handle);把router.js改为:
function route(handle, pathname) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
handle[pathname]();
} else {
console.log("No request handler found for " + pathname);
}
}
exports.route = route;创建requestHandlers.js为:
function start() {
console.log("Request handler 'start' was called.");
}
function upload() {
console.log("Request handler 'upload' was called.");
}
exports.start = start;
exports.upload = upload;然后重新运行服务器,然后访问:
和
输出为:
D:\tmp\tmp_dev_root\node.js>node index.js Server has started. Request for /start received. About to route a request for /start Request handler 'start' was called. Request for /test received. About to route a request for /test No request handler found for /test
11.再试试那个不好的方式。
把requestHandlers.js改为:
function start() {
console.log("Request handler 'start' was called.");
return "Hello Start";
}
function upload() {
console.log("Request handler 'upload' was called.");
return "Hello Upload";
}
exports.start = start;
exports.upload = upload;把router.js改为:
function route(handle, pathname) {
console.log("About to route a request for " + pathname);
if (typeof handle[pathname] === 'function') {
return handle[pathname]();
} else {
console.log("No request handler found for " + pathname);
return "404 Not found";
}
}
exports.route = route;把server.js改为:
var http = require("http");
var url = require("url");
function start(route, handle) {
function onRequest(request, response) {
var pathname = url.parse(request.url).pathname;
console.log("Request for " + pathname + " received.");
response.writeHead(200, {"Content-Type": "text/plain"});
var content = route(handle, pathname)
response.write(content);
response.end();
}
http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}
exports.start = start;然后都可以正常执行预期的输出:
http://localhost:8888/start -> Hello start
http://localhost:8888/upload -> Hello upload
http://localhost:8888/foo -> 404 Not found
12.把requestHandlers.js改为:
function start() {
console.log("Request handler 'start' was called.");
function sleep(milliSeconds) {
var startTime = new Date().getTime();
while (new Date().getTime() < startTime + milliSeconds);
}
sleep(10000);
return "Hello Start";
}
function upload() {
console.log("Request handler 'upload' was called.");
return "Hello Upload";
}
exports.start = start;
exports.upload = upload;然后测试了一下,果然访问:
和访问
类似,都是过了10秒后才输出。
13.然后把requestHandlers.js改为:
var exec = require("child_process").exec;
function start() {
console.log("Request handler 'start' was called.");
var content = "empty";
exec("ls -lah", function (error, stdout, stderr) {
content = stdout;
});
return content;
}
function upload() {
console.log("Request handler 'upload' was called.");
return "Hello Upload";
}
exports.start = start;
exports.upload = upload;果然重启服务器后,访问:
得到的是empty。
14.继续安装教程,测试了一下start不会影响到upload的情况,的确是可以的。
15.然后也测试了所谓耗时的find /,但是由于我此处好像根目录下啥都木有是,所以没有任何输出,所以用修改后的代码测试:
把requestHandlers.js改为:
var exec = require("child_process").exec;
function start(response) {
console.log("Request handler 'start' was called.");
exec("find /",
{ timeout: 10000, maxBuffer: 20000*1024 },
function (error, stdout, stderr) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write(stdout + "done find /");
response.end();
});
}
function upload(response) {
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello Upload");
response.end();
}
exports.start = start;
exports.upload = upload;然后才能看到输出:done find /
16.分别测试了小数量数据和大批量数据的上传。
17.去安装formidable:
CLi@PC-CLI-1 /cygdrive/d/tmp/tmp_dev_root/node.js $ npm install formidable npm http GET https://registry.npmjs.org/formidable npm http 200 https://registry.npmjs.org/formidable npm http GET https://registry.npmjs.org/formidable/-/formidable-1.0.11.tgz npm http 200 https://registry.npmjs.org/formidable/-/formidable-1.0.11.tgz formidable@1.0.11 node_modules\formidable
18.然后参考教程改好所有的代码,然后去上传文件,结果点击upload后,结果出现
cross-device link not permitted
方面的错误,解决过程参考:
【已解决】Node.js中所用的fs.renameSync出错:Error: EXDEV, cross-device link not permitted
至此,折腾基本结束。
转载请注明:在路上 » 【记录】折腾Node.js