MEAN ¡Stack ¡
Jay ¡Urbain, ¡PhD ¡
¡
- h6p://expressjs.com/ ¡
MEAN Stack Jay Urbain, PhD h6p://expressjs.com/ - - PowerPoint PPT Presentation
MEAN Stack Jay Urbain, PhD h6p://expressjs.com/ Requirements for a modern web app Low latency, fast response Ime Minimize page reloads
“MEAN ¡is ¡a ¡fullstack ¡JavaScript ¡plaWorm ¡for ¡modern ¡web ¡applicaIons” ¡ ¡ – ¡Mean.io ¡ ¡
and ¡many ¡more… ¡
h6p://www.youtube.com/watch?v=r1A1VR0ibIQ ¡ ¡
– Open ¡Source ¡and ¡Web ¡Standards ¡ – Consistent ¡models ¡across ¡stack ¡(backend ¡to ¡frontend) ¡
– Use ¡a ¡uniform ¡language ¡throughout ¡your ¡stack ¡
the ¡database ¡ – Very ¡small ¡memory ¡footprint, ¡low ¡processing ¡overhead ¡ – Leverage ¡JavaScript’s ¡popularity ¡ – Huge ¡community ¡
– Consistent ¡models ¡across ¡stack ¡increases ¡coupling, ¡reduces ¡separaIon ¡
– Is ¡JSON ¡text ¡the ¡most ¡efficient ¡way ¡to ¡store, ¡and ¡exchange ¡data? ¡Do ¡ you ¡want ¡your ¡front ¡end ¡to ¡be ¡dependent ¡on ¡your ¡database ¡schema? ¡ Store ¡large ¡numeric ¡datasets ¡as ¡text??? ¡ – Use ¡of ¡a ¡uniform ¡language ¡throughout ¡your ¡stack ¡– ¡is ¡JS ¡opImal ¡ server-‑side ¡sooware. ¡Best ¡for ¡relaIonal ¡database ¡queries ¡like ¡SQL? ¡ – Service ¡interfaces ¡can ¡reduce ¡performance, ¡i.e., ¡increase ¡latency ¡ – Mongo ¡really ¡does ¡not ¡support ¡transacIons ¡(record-‑level) ¡– ¡not ¡true ¡ transacIons, ¡lacks ¡ACID ¡transacIon ¡properIes ¡
Checkout ¡h6p://stackoverflow.com/research/developer-‑survey-‑2016 ¡for ¡latest ¡survey ¡results ¡
Several ¡popular ¡Node.js ¡frameworks ¡are ¡built ¡on ¡Express: ¡
end ¡REST ¡APIs. ¡
Microservices ¡using ¡ExpressJS. ¡
reusable ¡components ¡
function ¡myFunMiddleware(request, ¡response, ¡next) ¡{ ¡ ¡ ¡ ¡ ¡// ¡Do ¡stuff ¡with ¡the ¡request ¡and ¡response. ¡ ¡ ¡ ¡ ¡// ¡When ¡we're ¡all ¡done, ¡call ¡next() ¡to ¡defer ¡ ¡ ¡ ¡ ¡ ¡// ¡to ¡the ¡next ¡middleware. ¡ ¡ ¡ ¡ ¡next(); ¡ } ¡
var ¡connect ¡= ¡require("connect"); ¡ var ¡http ¡ ¡ ¡ ¡= ¡require("http"); ¡ var ¡app ¡ ¡ ¡ ¡ ¡= ¡connect(); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ http.createServer(app).listen(1337); ¡ ¡// ¡Add ¡some ¡middleware ¡ ¡app.use(connect.logger()); ¡ ¡app.use(connect.Security); ¡ ¡app.use(connect.Routing); ¡ ¡... ¡
// ¡Require ¡what ¡we ¡need ¡ var ¡http ¡= ¡require("http"); ¡ ¡ // ¡Build ¡the ¡server ¡ var ¡app ¡= ¡http.createServer( ¡ ¡function ¡(request, ¡response) ¡{ ¡ ¡ ¡response.writeHead(200, ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡"Content-‑Type": ¡"text/plain" ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡response.end("Hello ¡world!"); ¡ ¡ ¡ ¡ ¡}); ¡ ¡ // ¡Start ¡that ¡server ¡ app.listen(1337, ¡"localhost"); ¡ console.log("Server ¡running ¡at ¡http://localhost:1337/"); ¡
var ¡http ¡= ¡require("http"); ¡ http.createServer(function ¡(req, ¡res) ¡{ ¡ ¡
¡ ¡ ¡ ¡ ¡ ¡// ¡Homepage ¡ ¡ ¡ ¡ ¡ ¡ ¡if ¡(req.url ¡== ¡"/") ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡res.writeHead(200, ¡{ ¡"Content-‑Type": ¡"text/html" ¡}); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡res.end("Welcome ¡to ¡the ¡homepage!"); ¡ ¡ ¡ ¡ ¡ ¡ ¡}// ¡About ¡page ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡if ¡(req.url ¡== ¡"/about") ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡res.writeHead(200, ¡{ ¡"Content-‑Type": ¡"text/html" ¡}); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡res.end("Welcome ¡to ¡the ¡about ¡page!"); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡ ¡ ¡ ¡ ¡ ¡ ¡// ¡404'd! ¡ ¡ ¡ ¡ ¡ ¡ ¡else ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡res.writeHead(404, ¡{ ¡"Content-‑Type": ¡"text/plain" ¡}); ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡res.end("404 ¡error! ¡File ¡not ¡found."); ¡ ¡ ¡ ¡ ¡ ¡ ¡} ¡
¡ }).listen(1337, ¡"localhost"); ¡
request ¡to ¡a ¡parIcular ¡endpoint, ¡which ¡is ¡a ¡URI ¡(or ¡path) ¡and ¡a ¡specific ¡ HTTP ¡request ¡method ¡(GET, ¡POST, ¡and ¡so ¡on). ¡
when ¡the ¡route ¡is ¡matched. ¡ Route ¡definiIon ¡takes ¡the ¡following ¡structure: ¡
Where: ¡
app.get('/', ¡funcIon ¡(req, ¡res) ¡{ ¡ ¡ ¡res.send('Hello ¡World!') ¡ }) ¡ ¡
app.post('/', ¡funcIon ¡(req, ¡res) ¡{ ¡ ¡ ¡res.send('Got ¡a ¡POST ¡request') ¡ }) ¡ ¡
app.put('/user', ¡funcIon ¡(req, ¡res) ¡{ ¡ ¡ ¡res.send('Got ¡a ¡PUT ¡request ¡at ¡/user') ¡ }) ¡ ¡
app.delete('/user', ¡funcIon ¡(req, ¡res) ¡{ ¡ ¡ ¡res.send('Got ¡a ¡DELETE ¡request ¡at ¡/user') ¡ }) ¡ h6ps://expressjs.com/en/guide/rouIng.html ¡
app.get('/users/:id?', ¡function ¡(req, ¡res, ¡next) ¡ { ¡ ¡ ¡ ¡ ¡var ¡id ¡= ¡req.params.id; ¡ ¡ ¡ ¡ ¡if ¡(id) ¡{ ¡ ¡ ¡ ¡ ¡// ¡do ¡something ¡ ¡ ¡ ¡ ¡} ¡else ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡next(); ¡ ¡ ¡ ¡ ¡} ¡ }); ¡ { ¡ ¡ ¡ ¡ ¡ ¡path: ¡'/user/:id?', ¡ ¡ ¡ ¡ ¡method: ¡'all' ¡| ¡'get' ¡| ¡'post' ¡| ¡'put' ¡| ¡'delete', ¡ ¡ ¡ ¡ ¡callbacks: ¡[ ¡[Function] ¡], ¡ ¡ ¡ ¡ ¡keys: ¡[ ¡{ ¡name: ¡'id', ¡optional: ¡true ¡} ¡], ¡ ¡ ¡ ¡ ¡regexp: ¡/^\/user(?:\/([^\/]+?))?\/?$/i, ¡ ¡ ¡ ¡ ¡params: ¡[ ¡id: ¡'12' ¡] ¡ ¡ } ¡
// ¡home.js ¡file ¡in ¡Routing ¡folder. ¡ module.exports ¡= ¡function ¡(app) ¡{ ¡ ¡ ¡ ¡ ¡// ¡home ¡page ¡ ¡ ¡ ¡ ¡app.get('/', ¡function ¡(req, ¡res) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡res.render('index', ¡{ ¡title: ¡'Home ¡Page. ¡ ¡' ¡}) ¡ ¡ ¡ ¡ ¡}); ¡ ¡ ¡ ¡ ¡// ¡about ¡page ¡ ¡ ¡ ¡ ¡app.get('/about', ¡function ¡(req, ¡res) ¡{ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡res.render('about', ¡{ ¡title: ¡'About ¡Me. ¡ ¡' ¡}) ¡ ¡ ¡ ¡ ¡}); ¡ } ¡ var ¡express ¡= ¡require("express"); ¡ var ¡http ¡ ¡ ¡ ¡= ¡require("http"); ¡ var ¡app ¡ ¡ ¡ ¡ ¡= ¡express(); ¡ ¡ // ¡Include ¡a ¡route ¡file ¡require('./ routes/home')(app); ¡ ¡ http.createServer(app).listen(1337); ¡
app.use( ¡express.bodyParser() ¡); ¡
applicaIon ¡skeleton. ¡
Pug ¡is ¡a ¡template ¡engine ¡ implemented ¡with ¡JavaScript ¡for ¡ Node.js ¡and ¡browsers ¡