首页 > 代码库 > node.js笔记

node.js笔记

最近在读<Node入门经典>, 之前没有做笔记, 今天开始把看过自己又写了的代码放这里以免忘记.

 

express

技术分享
var express = require(‘express‘)
var app = express()

app.get(‘/‘, function (req, res) {
  res.send(‘Hello World‘)
})
app.listen(3000)
View Code

 

route

技术分享
var http = require(‘http‘);
var url = require(‘url‘);

http.createServer(function(req,res){
	
	var name = url.parse(req.url).pathname;
	if (name === ‘/‘){
		res.writeHead(200,{
			‘Content-Type‘:‘text/plain‘
		});
		res.end(‘home page‘);
	}
	else if (name === ‘/about‘){
		res.writeHead(200,{
			‘Content-Type‘:‘text/plain‘
		});
		res.end(‘about me‘);
	}
	else if (name === ‘/redirect‘)
	{
		res.writeHead(301,{
		‘location‘: ‘http://www.baidu.com‘
		});
		res.end();
	}
	else{
		res.writeHead(404,{
			‘Content-Type‘:‘text/plain‘
		});
		res.end(‘page not find‘);
	}	
	
	
	
}).listen(3000, "127.0.0.1");

console.log(‘server start‘);
View Code

 

socket.io

技术分享
var app = require(‘express‘)();
var http = require(‘http‘).Server(app);
var io = require(‘socket.io‘)(http);

app.get(‘/‘, function(req, res){
  res.sendfile(‘index2.html‘);
});

io.on(‘connection‘, function(socket){
  console.log(‘a user connected‘);
  socket.emit(‘chat message‘, {text:‘hi‘});

  socket.on(‘disconnect‘, function(){
    console.log(‘user disconnected‘);
  });
  
  socket.on(‘chat message‘, function(msg){
    console.log(‘message: ‘ + msg);
  });
});

http.listen(3000, function(){
  console.log(‘listening on *:3000‘);
});
View Code
技术分享
<!doctype html>
<html>
  <head>
    <title>Socket.IO chat</title>    
  </head>
  <body>
    <p id=‘count‘></p>
    <script src="/socket.io/socket.io.js"></script>

    <script>
      var socket = io();
      var count = document.getElementById(count);
      socket.on(chat message, function(data){
        count.innerHTML = data.text;
      });

</script>
  </body>
</html>
View Code

 

json

技术分享
var http = require(‘http‘);

var obj = {
	name:‘green‘,
	sur:‘abcd‘
};
console.log(obj);

var json = JSON.stringify(obj);
console.log(json);

var ori = JSON.parse(json);
console.log(ori);
View Code

 

spawn

技术分享
var spawn = require(‘child_process‘).spawn;
var ping = spawn(‘ping‘, [‘www.baidu.com‘]);
ping.stdout.setEncoding(‘utf8‘);
ping.stdout.on(‘data‘, function(data){
	console.log(data);
});
ping.on(‘exit‘, function(code, signal){
	console.log(code,  ‘ ‘, signal);
	process.exit(0);
});
setInterval(function(){
	ping.kill(‘SIGINT‘);	
},	2000);
View Code

 

cluster

技术分享
var cluster = require(‘cluster‘);
var http = require(‘http‘);
var cpus = 4;
if (cluster.isMaster){
	console.log(‘master start at ‘ + process.pid);
	
	for (var i = 0; i < cpus; ++i ){
		cluster.fork();		
	}
	cluster.on(‘death‘, function(worker){
		console.log(‘die: ‘ + woker.pid);
		cluster.fork();
	});
}
else{
	console.log(‘worker start ‘ + process.pid);	
View Code

 

events

技术分享
var eventEm = require(‘events‘).EventEmitter;
var myEvent = new eventEm();

myEvent.on(‘message‘, function(data){
	console.log(data);
});

myEvent.on(‘destroy‘, function(){
	console.log(‘destroyed‘);
});

myEvent.emit(‘message‘,‘show message‘);
setTimeout(function(){
	myEvent.emit(‘destroy‘);
}, 1000);
View Code

 

stream

技术分享
var http = require(‘http‘);
var fs = require(‘fs‘);

http.createServer(function(req,res){
	var mp3 = ‘abc.mp3‘;
	var stat = fs.statSync(mp3);
	
	res.writeHead(200,
	{
		‘Content-Type‘:‘audio/mpeg‘,
		‘Content-Length‘: stat.size
	});
	var readSteam = fs.createReadStream(mp3);
	readSteam.pipe(res);
	
}).listen(3000, "127.0.0.1");

console.log(‘server start‘);
View Code

 

node.js笔记