node. JS Notes

Source: Internet
Author: User

Recently read the <node Introduction Classic, and did not take notes, today began to read their own written code here to avoid forgetting.

Express

var express = require (' Express ') var app = Express () app.get ('/', function (req, res) {  res.send (' Hello World ')}) app.li Sten (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 ("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 (The function () {  console.log (' listening on *:3000 ');});
View Code
<!DOCTYPE HTML><HTML>  <Head>    <title>Socket.io Chat</title>      </Head>  <Body>    <PID= ' Count '></P>    <Scriptsrc= "/socket.io/socket.io.js"></Script>    <Script>      varSocket=io (); varCount=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 ']);p ing.stdout.setEncoding (' UTF8 ') );p ing.stdout.on (' Data ', function (data) {Console.log (data);}); Ping.on (' Exit ', function (code, signal) {Console.log (Code,  ', signal);p rocess.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 a T ' + 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.st Atsync (mp3); Res.writehead (200,{' content-type ': ' Audio/mpeg ', ' content-length ': stat.size}); var readsteam = Fs.createreadstream (mp3); readsteam.pipe (res);}). Listen ("127.0.0.1"); Console.log (' Server start ');
View Code

node. JS Notes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.