node.jsの入力ストリーム
6874 ワード
//
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
');
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));
})
})
Webサーバの作成
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 + '
');
})