一、使用connect框架
.use方法用於綁定中間件到connect服務器,它會配置一系列在接到請求時調用的中間件模塊,此例中咱們要配置的中間件有favicon logger static router
app.get/post/put 寫法:app.requestName('path', function(req, res, next){});html
app-connect.jsnode
var connect = require('connect'); //npm install connect connect.createServer() .use(connect.favicon()) .use(conect.logger()) .use('/filez', connect.static(__dirname + '/filez')) .use(connect.router(function(app){ app.get('/', require('./home-node').get); //一個URL字符串和兩個函數類型的參數 //路由器配置函數能夠包含不限數量的函數,你能夠爲本身的應用構造一個處理函數的隊列 app.get('/square', htutil.loadParams, require('./square-node').get); app.get('/factorial', htutil.loadParams, require('./factorial-node').get); app.get('/fibonacci', htutil.loadParams, require('./fibo2-node').get); app.get('/mult', htutil.loadParams, require('./mult-node').get); })).listen(3000); console.log('listening to http://localhost:3000');
二、使用express框架web
Express框架是一個基於connect(一箇中間件框架)的web應用框架
Express專一於構建一個應用,包括提供一個模板系統;connect專一於作web服務的基礎設施express
安裝Express和EJS(模塊處理系統) npm install express ejsnpm
app-express.js瀏覽器
var htutil = require('./htutil'); var math = require('./math'); var express = require('express'); //var app = express.createServer(express.logger()); //express 2.X var app = express(); //express 3.X //可選,由於Express下默認爲CWD/views app.set('views', __dirname + '/views'); app.engine('.html', require('ejs').__express); app.set('view engine', 'ejs'); app.configure(function(){ app.use(app.router); app.use(express.static(__dirname + '/filez')); //默認的錯誤處理函數,顯示棧軌跡 //若是要顯示用戶友好的錯誤,app.err(function(err, req, res, next){ // res.send(error page); //or res.render('template'); // }); app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); /* 改爲下面的話,瀏覽器會顯示一個簡單的消息-Internal Server Error內部服務器錯誤 app.use(express.errorHandler({ dumpExceptions: true })); */ }); //以上配置了必需的中間件,由於這裏展現的配置項對應的是模板系統的配置,全部.html文件會由EJS引擎處理 //如下是路由器配置 app.get('/', function(req, res){ res.render('home.html', {title: "Math Wizard"}); }); app.get('/mult', htutil.loadParams, function(req, res){ if (req.a && req.b) req.result = req.a * req.b; res.render('mult.html', {title: "Math Wizard", req: req}); }); app.get('/square', htutil.loadParams, function(req, res){ if (req.a) req.result = req.a * req.a; res.render('square.html', {title: "Math Wizard", req: req}); }); app.get('/fibonacci', htutil.loadParams, function(req, res){ if (req.a){ math.fibonacciAsync(Math.floor(req.a), function(val){ req.result = val; res.render('fibo.html', {title: "Math Wizard", req: req}); }); }else { res.render('fibo.html', {title: "Math Wizard", req: req}); } }); app.get('/factorial', htutil.loadParams, function(req, res){ if (req.a) req.result = math.factorial(req.a); res.render('factorial.html', {title: "Math Wizard", req: req}); }); app.get('/404', function(req, res){ res.send('NOT FOUND' + req.url); }); //res.render函數經過一個模板文件渲染數據,EJS只是Express裏衆多模板引擎中的一個 //配置目的是讓EJS可以爲views目錄下的全部.html文件服務 /*Express裏還有其餘一些模板引擎 res.render('index.haml', {..data..}); 使用Haml res.render('index.jade', {..data..}); 使用Jade res.render('index.ejs', {..data..}); 使用EJS res.render('index.coffee', {..data..}); 使用CoffeeKup res.render('index.jqtpl', {..data..}); 使用jQueryTemplates 也能夠經過 app.set('view engine', 'haml'); app.set('view engine', 'jade'); 方法來改變默認的渲染引擎 layout.html 默認狀況下,模板中用於渲染的內容會被命名爲body,而後傳遞到layout模板中,當app-express.js調用 res.render('fibo.html'...)時,它會先用fibo.html渲染對應的頁面片斷,而後再使用layout模板渲染整個頁面 有兩種方法覆蓋這一默認的行爲 一、在Express裏建立一個全局的配置,經過這個全局配置來控制layout模板的啓用與禁用 app.set('view options', {layout: false(or true)}); 二、覆蓋layout模板對應的渲染方式或者使用不一樣的layout模板 res.render('myview.ejs', {layout: false(or true)}); 或者res.render('page', {layout: 'mylayout.jade'}); <% code %> Javascript代碼 <%= code %> 顯示替換過HTML特殊字符的內容 <%- code %> 顯示原始HTML內容 */ app.listen(3000); console.log('listening to http://localhost:3000');
html頁面放在views目錄下服務器
layout.htmlapp
<!DOCTYPE html> <html> <head> <title></title> </head> <body> <h1><%=title%></h1> <table> <tr> <td> <div class="navbar"> <p><a href="/">home</a></p> <p><a href="/mult">Multiplication</a></p> <p><a href="/square">Square</a></p> <p><a href="/factorial">Factorial</a></p> <p><a href="/fibonacci">Fibonacci</a></p> </div> </td> <td></td> </tr> </table> </body> </html>
home.html框架
<% include layout.html %> <p>Math Wizard</p>
mult.html函數
<% include layout.html %> <% if (req.a && req.b){ %> <p class="result"> <%=req.a%> * <%=req.b%> = <%=req.result%> </p> <% } %> <p>Enter numbers to multiply</p> <form name="mult" action="/mult" method="get"> A: <input type="text" name="a" /><br/> B: <input type="text" name="b" /> <input type="submit" name="Submit" /> </form>
還有其餘一些頁面就不一一列出來了,都大同小異