On Github purplecode / lightning-talk-nodejs
var http = require('http'); var handleRequest = function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hit: ' + req.url); }; http.createServer(handleRequest).listen(3000, function () { console.log("Server listening on: http://localhost:3000"); });
var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello GET'); }); app.post('/', function (req, res) { res.send('Hello POST!'); }); app.listen(3000, function () { console.log("Server listening on: http://localhost:3000"); });
var express = require('express'); var app = express(); app.use('/static', express.static(__dirname + 'public')); app.listen(3000, function () { console.log("Server listening on: http://localhost:3000"); });
var express = require('express'); var app = express(); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.get('/', function(req, res){ res.render("index", {message: 'Hello world!'}); }); app.listen(3000, function () { console.log("Server listening on: http://localhost:3000"); });views/index.jade
html head title title body h1= message
npm install express-generator -g express myapp
npm install forever -g forever start app.js
NODE_ENV=production forever start -l forever.log -o out.log -e err.log app.js
forever list
forever stop app.js
forever stopall
const cluster = require('cluster'); const http = require('http'); const numberOfCpus = require('os').cpus().length; function spawnMaster() { for (var i = 0; i < numberOfCpus; i++) { cluster.fork(); } cluster.on('exit', (worker, code, signal) => { console.log(`worker ${worker.process.pid} died`); }); } function spawnFork() { // Workers can share any TCP connection, in this case it is an HTTP server http.createServer((req, res) => { res.writeHead(200); res.end('hello world\n'); }).listen(8000); } cluster.isMaster ? spawnMaster() : spawnFork();