最近在读<Node入门经典>, 之前没有做笔记, 今天开始把看过自己又写了的代码放这里以免忘记.
express
var express = require('express')
var app = express() app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(3000)
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');
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');
});
<!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>
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);
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);
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);
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);
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');