Node是:
构建在JavaScript之上的,事件触发和异步的;专为数据密集型实时程序设计的。
组成部分:
var http =require('http'); 引入required模块
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/');
EventEmitter 类
var event = new EventEmitter ();
var buf = new Buffer ( "www.runoob.com" , "utf-8" ); 创建
Stream(流)
从流中读取数据:var fs = require("fs");
var data = '';
// 创建可读流
var readerStream = fs.createReadStream('input.txt');
// 设置编码为 utf8。
readerStream.setEncoding('UTF8');
// 处理流事件 --> data, end, and error
readerStream.on('data', function(chunk) {
data += chunk;
});
readerStream.on('end',function(){
console.log(data);
});
readerStream.on('error', function(err){
console.log(err.stack);
});
console.log("程序执行完毕");
写入流:
var fs = require("fs"); var data = '菜鸟教程官网地址:www.runoob.com'; // 创建一个可以写入的流,写入到文件 output.txt 中 var writerStream = fs.createWriteStream('output.txt'); // 使用 utf8 编码写入数据 writerStream.write(data,'UTF8'); // 标记文件末尾 writerStream.end(); // 处理流事件 --> data, end, and error writerStream.on('finish', function() { console.log("写入完成。"); }); writerStream.on('error', function(err){ console.log(err.stack); }); console.log("程序执行完毕");路由:
var http = require("http"); var url = require("url"); function start() { function onRequest(request, response) { var pathname = url.parse(request.url).pathname; console.log("Request for " + pathname + " 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;router.js 的文件
function route(pathname) { console.log("About to route a request for " + pathname); } exports.route = route;扩展一下服务器的start()函数,以便将路由函数作为参数传递过去,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;
相应扩展index.jsvar server = require("./server");
var router = require("./router");
server.start(router.route);
__filename :表示当前正在执行的脚本的文件名。它将输出文件所在位置的绝对路径,且和命令行参数所指
定的文件名不一定相同。 如果在模块中,返回的值是模块文件的路径。
__dirname :表示当前执行脚本所在的目录。
常用工具:
util 是一个Node.js 核心模块,提供常用函数的集合 用法:
var util = require('util');
function Base() {
this.name = 'base';
this.base = 1991;
this.sayHello = function() {
console.log('Hello ' + this.name);
};
}
Base.prototype.showName = function() {
console.log(this.name);
};
function Sub() {
this.name = 'sub';
}
util.inherits(Sub, Base);
var objBase = new Base();
objBase.showName();
objBase.sayHello();
console.log(objBase);
var objSub = new Sub();
objSub.showName();
//objSub.sayHello();
console.log(objSub);
文件 系统:
fs.open(path, flags[, mode], callback) 打开文件
fs.stat(path, callback) 读取文件信息
fs.writeFile(filename, data[, options], callback) 写入文件
fs.read(fd, buffer, offset, length, position, callback) 读取文件
fs.close(fd, callback) 关闭文件
fs.ftruncate(fd, len, callback) 截取文件
fs.unlink(path, callback) 删除文件
获取GET请求内容:
获取 URL 的参数:
获取 POST 请求内容:
varhttp = require('http');varquerystring = require('querystring');
http.createServer(function(req,res){
// 定义了一个post变量,用于暂存请求体的信息
varpost = '';
// 通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中
req.on('data',function(chunk){
post += chunk;});
// 在end事件触发后,通过querystring.parse将post解析为真正的POST请求格式,然后向客户端返回。
req.on('end',function(){
post = querystring.parse(post);
res.end(util.inspect(post));});}).listen(3000);
Express 框架:
express.static 中间件来设置静态文件路径:
app.use(express.static('public'));将图片, CSS, JavaScript 文件放在 public 目录下
child_process 模块: 创建子进程
exec()方法:child_process.exec(command[, options], callback)
spawn()方法:child_process.spawn(command[, args][, options])
fork()方法:child_process.fork(modulePath[, args][, options])
连接 MySQL:
连接 MongoDB: