从零开始学习Node.js系列教程之基于connect和express框架的多页面实现数学运算示例
本文实例讲述了Node.js基于connect和express框架的多页面实现数学运算。分享给大家供大家参考,具体如下:
1、使用connect框架
.use方法用于绑定中间件到connect服务器,它会配置一系列在接到请求时调用的中间件模块,此例中我们要配置的中间件有faviconloggerstaticrouter
app.get/post/put 写法:app.requestName('path',function(req,res,next){});
app-connect.js
varconnect=require('connect');//npminstallconnect
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('listeningtohttp://localhost:3000');
2、使用express框架
Express框架是一个基于connect(一个中间件框架)的web应用框架
Express专注于构建一个应用,包括提供一个模板系统;connect专注于做web服务的基础设施
安装Express和EJS(模块处理系统)npminstallexpressejs
app-express.js
varhtutil=require('./htutil');
varmath=require('./math');
varexpress=require('express');
//varapp=express.createServer(express.logger());//express2.X
varapp=express();//express3.X
//可选,因为Express下默认为CWD/views
app.set('views',__dirname+'/views');
app.engine('.html',require('ejs').__express);
app.set('viewengine','ejs');
app.configure(function(){
app.use(app.router);
app.use(express.static(__dirname+'/filez'));
//默认的错误处理函数,显示栈轨迹
//如果要显示用户友好的错误,app.err(function(err,req,res,next){
//res.send(errorpage);//orres.render('template');
//});
app.use(express.errorHandler({
dumpExceptions:true,showStack:true
}));
/*
改成下面的话,浏览器会显示一个简单的消息-InternalServerError内部服务器错误
app.use(express.errorHandler({
dumpExceptions:true
}));
*/
});
//以上配置了必需的中间件,因为这里展示的配置项对应的是模板系统的配置,所有.html文件会由EJS引擎处理
//以下是路由器配置
app.get('/',function(req,res){
res.render('home.html',{title:"MathWizard"});
});
app.get('/mult',htutil.loadParams,function(req,res){
if(req.a&&req.b)req.result=req.a*req.b;
res.render('mult.html',{title:"MathWizard",req:req});
});
app.get('/square',htutil.loadParams,function(req,res){
if(req.a)req.result=req.a*req.a;
res.render('square.html',{title:"MathWizard",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:"MathWizard",req:req});
});
}else{
res.render('fibo.html',{title:"MathWizard",req:req});
}
});
app.get('/factorial',htutil.loadParams,function(req,res){
if(req.a)req.result=math.factorial(req.a);
res.render('factorial.html',{title:"MathWizard",req:req});
});
app.get('/404',function(req,res){
res.send('NOTFOUND'+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('viewengine','haml');
app.set('viewengine','jade');方法来改变默认的渲染引擎
layout.html
默认情况下,模板中用于渲染的内容会被命名为body,然后传递到layout模板中,当app-express.js调用
res.render('fibo.html'...)时,它会先用fibo.html渲染对应的页面片段,然后再使用layout模板渲染整个页面
有两种方法覆盖这一默认的行为
1、在Express里创建一个全局的配置,通过这个全局配置来控制layout模板的启用与禁用
app.set('viewoptions',{layout:false(ortrue)});
2、覆盖layout模板对应的渲染方式或者使用不同的layout模板
res.render('myview.ejs',{layout:false(ortrue)});
或者res.render('page',{layout:'mylayout.jade'});
<%code%>Javascript代码
<%=code%>显示替换过HTML特殊字符的内容
<%-code%>显示原始HTML内容
*/
app.listen(3000);
console.log('listeningtohttp://localhost:3000');
html页面放在views目录下
layout.html
<%=title%>
|
|
home.html
<%includelayout.html%>MathWizard
mult.html
<%includelayout.html%>
<%if(req.a&&req.b){%>
<%=req.a%>*<%=req.b%>=<%=req.result%>
<%}%>
Enternumberstomultiply
A:
B:
还有其他一些页面就不一一列出来了,都大同小异
希望本文所述对大家nodejs程序设计有所帮助。