首页 > 代码库 > 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)
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‘);
node.js笔记
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。