Download presentation
Presentation is loading. Please wait.
1
Web development with ExpressJS Learning & Development http://academy.telerik.com Telerik School Academy
2
1. Connect for NodeJS 2. ExpressJS 3. Views and layout 4. Working with Data 5. Common and Advanced Scenarios 2
3
Event-Driven, Asynchronous IO, Server-Side JavaScript library in C Open Source Available on Windows Service Under IIS (iisnode) *nix systems As a service Azure Heroku 3
4
Basic server implementation var http = require('http'); http.createServer(function(req, res) { res.writeHead(200, { res.writeHead(200, { 'Content-Type': 'text/plain' 'Content-Type': 'text/plain' }); //return success header }); //return success header res.write('My server is running! ^_^'); //response res.write('My server is running! ^_^'); //response res.end(); //finish processing current request res.end(); //finish processing current request}).listen(1234); 4
5
Connect is a middleware framework for node Built on top of node’s Http Server http://www.senchalabs.org/connect/ http://www.senchalabs.org/connect/ 5 var connect = require('connect'); var app = connect().use(connect.logger('dev')).use(connect.logger('dev')).use(connect.static('public')).use(connect.static('public')).use(function(req, res){.use(function(req, res){ res.end('hello world\n'); res.end('hello world\n'); }) })http.createServer(app).listen(3000); $ npm install connect
6
Request Processing Pipeline 6 ResponseResponseRequestRequest Middleware (logging, authentication, etc.)
7
Custom middleware function for connect 7 var connect = require('connect'), util = require('util'); util = require('util'); var interceptorFunction = function(request, response, next) { console.log(util.format('Request for %s with method %s', request.url, request.method)); console.log(util.format('Request for %s with method %s', request.url, request.method)); next(); next();}; var app = connect() //.use('/log', interceptorFunction) //.use('/log', interceptorFunction).use(interceptorFunction).use(interceptorFunction).use(function onRequest(request, response) {.use(function onRequest(request, response) { response.end('Hello from Connect!'); response.end('Hello from Connect!'); }).listen(3001); }).listen(3001);
8
Built on top of Connect Middleware Adds functionality to Connect Request / Response enhancements Routing View Support HTML Helpers Content Negotiation Exposes Connect Middleware 8
9
Request ViewFileXML JSON etc. Function Routing 9
10
10 var express = require('express'); var app = express(); app.get('/', function (request, response) { response.send('Welcome to Express!'); response.send('Welcome to Express!');}); app.get('/customer/:id', function (req, res) { res.send('Customer requested is ' + req.params['id']); });app.listen(3000);
11
Simple ExpressJS application and "nodemon" Simple ExpressJS application and "nodemon" Create routes and require() them Create routes and require() them Pass parameters Pass parameters Configure and middleware Configure and middleware
12
User Interface Based on Templates Support for multiple View Engines Jade, EJS, JSHtml,... Default is Jade http://jade-lang.com http://jade-lang.com 12 app.get('/', function (req, res) { res.render('index'); res.render('index');});
13
var express = require('express'), path = require('path'); path = require('path'); var app = express(); app.configure(function () { app.set('views', __dirname + '/views'); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.set('view engine', 'jade'); app.use(express.static(path.join(__dirname, 'public'))); app.use(express.static(path.join(__dirname, 'public')));}); app.get('/', function (req, res) { res.render('empty'); res.render('empty');});app.listen(3000); doctypehtml(lang="en") head head title Welcome to this emtpy page title Welcome to this emtpy page body body 13
14
Show simple views in ExpressJS Show simple views in ExpressJS Jade syntax examples Jade syntax examples Layouts and blocks Layouts and blocks Stylus Stylus
15
Pass data to the views Read data from the views (bodyParser) Read and send files Data for all views 15 res.render('index', { title: 'Customer List' }); var filePath = req.files.picture.path; //... res.download(filePath);res.sendfile(filePath); app.locals.clock = { datetime: new Date().toUTCString()};
16
Pass data to views (customer.index) Pass data to views (customer.index) Submit data from views (customer.create) Submit data from views (customer.create) Content negotiation (customer.details) Content negotiation (customer.details) Upload files (customer.create) Upload files (customer.create) Helpers (app.locals) Helpers (app.locals)
17
Cookies Cookies Sessions Sessions Custom middleware Custom middleware Authentication and authorization Authentication and authorization 17
18
Express.js Samples https://github.com/visionmedia/express https://github.com/visionmedia/express Database Support MS SQL CouchDB PostgreSQL Redis Socket.io Real-time support 18
19
Search on npm.org before you re-invent the wheel Express is Lightweight Framework and Fast to work with Testing is not optional Mocha JavaScript can get messy 19
20
форум програмиране, форум уеб дизайн курсове и уроци по програмиране, уеб дизайн – безплатно програмиране за деца – безплатни курсове и уроци безплатен SEO курс - оптимизация за търсачки уроци по уеб дизайн, HTML, CSS, JavaScript, Photoshop уроци по програмиране и уеб дизайн за ученици ASP.NET MVC курс – HTML, SQL, C#,.NET, ASP.NET MVC безплатен курс "Разработка на софтуер в cloud среда" BG Coder - онлайн състезателна система - online judge курсове и уроци по програмиране, книги – безплатно от Наков безплатен курс "Качествен програмен код" алго академия – състезателно програмиране, състезания ASP.NET курс - уеб програмиране, бази данни, C#,.NET, ASP.NET курсове и уроци по програмиране – Телерик академия курс мобилни приложения с iPhone, Android, WP7, PhoneGap free C# book, безплатна книга C#, книга Java, книга C# Николай Костов - блог за програмиране http://academy.telerik.com
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.