//读入流 var fs = require("fs"); var data = ""; //创建可读流 var readStreams = fs.createReadStream('D:\a.txt'); //设置编码为utf-8编码 readStreams.setEncoding('UTF-8'); //处理流事件 readStreams.on('data',function (chunk) { data += chunk; }); readStreams.on('end',function () { console.log(data); }); readStreams.on('error',function (err) { console.log(err.stack); }); console.log("程序执行完毕");
util框架
//util是一个node.js核心模块 提供常见函数的集合 //utl.inherts提供原型之间的继承 var util = require("util"); //定义了基础对象Base 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'; } //sub 继承自base 来进行 util.inherits(Sub,Base); var objName = new Base(); objName.showName(); objName.sayHello(); console.log(objName); var objSub = new Sub(); objSub.showName(); //objSub.sayHello(); console.log(objSub);
//将对象转化为字符串 var util = require('util'); function Person() { this.name = 'bb'; this.toString = function () { return this.name; }; } var obj = new Person(); console.log(util.inspect(obj)); console.log(util.inspect(obj,true));获取get的内容
//获取get请求中的内容 var http = require('http'); var url = require('url'); var util = require('util'); http.createServer(function (req,res) { res.writeHead(200,{'Content-Type':'text/plain'}); //用parse函数来进行解析 res.end(util.inspect(url.parse(req.url,true))); }).listen(3000);
var http = require('http'); var querystring = require('querystring'); var util = require('util'); http.createServer(function(req, res){ var post = ''; //定义了一个post变量,用于暂存请求体的信息 req.on('data', function(chunk){ //通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中 post += chunk; }); req.on('end', function(){ //在end事件触发后,通过querystring.parse将post解析为真正的POST请求格式,然后向客户端返回。 post = querystring.parse(post); res.end(util.inspect(post)); }); }).listen(3000);
//os模块提供一些基本的系统函数 var os = require('os'); console.log(os.type());net模块用于构建小型服务器等
var net = require('net'); var server = net.createServer(function(connection) { console.log('client connected'); connection.on('end', function() { console.log('客户端关闭连接'); }); connection.write('Hello World!\r\n'); connection.pipe(connection); }); server.listen(8080, function() { console.log('server is listening'); });
var net = require('net'); //连接到指定的地址和端口 var client = net.connect({port:8080},function() { console.log('连接到服务器'); }); client.on('data',function (data) { console.log(data.toString()); client.end; }); client.on('end',function () { console.log('断开与服务器的连接'); });
//dns用于解析域名 var dns = require('dns'); dns.lookup('www.github.com',function onLookup(err,address,family){ console.log('ip地址',address); dns.reverse(address,function (err,hostname) { if(err){ console.log(err.stack); } console.log('反向解析' + address + JSON.stringify(hostname)); }) })
var http = require('http'); var fs = require('fs'); var url = require('url'); // 创建服务器 http.createServer( function (request, response) { // 解析请求,包括文件名 var pathname = url.parse(request.url).pathname; // 输出请求的文件名 console.log("Request for " + pathname + " received."); // 从文件系统中读取请求的文件内容 fs.readFile(pathname.substr(1), function (err, data) { if (err) { console.log(err); // HTTP 状态码: 404 : NOT FOUND // Content Type: text/plain response.writeHead(404, {'Content-Type': 'text/html'}); }else{ // HTTP 状态码: 200 : OK // Content Type: text/plain response.writeHead(200, {'Content-Type': 'text/html'}); // 响应文件内容 response.write(data.toString()); } // 发送响应数据 response.end(); }); }).listen(8081); // 控制台会输出以下信息 console.log('Server running at http://127.0.0.1:8081/');
//创建web客户端 var http = require('http'); //用于请求的项 var options = { host:'localhost', port:'8081', path:'/index,htm', }; //处理响应的回调函数 var callback; callback = function (response) { //不断更新数据 var body = ''; response.on('data', function (data) { body += data; }); response.on('end', function () { //数据接收完成 console.log(body); }); //向服务端发送请求 var req = http.request(options, callback); req.end; };
由路由来设置访问的子页面
var express = require('express'); var app = express(); //主页输出hello world app.get('/',function (req,res) { res.send('hello world'); }); var sever = app.listen(3000,function () { var host = sever.address.address; var port = sever.address.port; console.log(host,port); }); //post 请求 // POST 请求 app.post('/', function (req, res) { console.log("主页 POST 请求"); res.send('Hello POST'); }); app.use(express.static('public')); //listuser页面的响应 app.get('/list_user',function (req,res) { res.send("用户列表页面"); })监听html的页面
<html> <body> <form action = "http://127.0.0.1:3000/process_get" method="GET"> FirstName:<input type = "text" name = "first_name"><br> LastName:<input type="text" name = "last_name"> <input type="submit" value="Submit"> </form> </body> </html>随后进行响应和返回
<html> <body> <form action = "http://127.0.0.1:3000/process_get" method="GET"> FirstName:<input type = "text" name = "first_name"><br> LastName:<input type="text" name = "last_name"> <input type="submit" value="Submit"> </form> </body> </html>
//设置路由参数 app.param('newId',function (req,res,next,newId) { req.newId = newId; next(); }); //设置参数 注意冒号 app.get('/news/:newId',function (req,res) { res.end('newId:' + req.newId + '\n'); })