node.js之express编程

来源:互联网 发布:Js undefunde 与 null 编辑:程序博客网 时间:2024/05/21 22:47

1.index.js

//test 1: 访问目录下的文件//浏览器输入: http://localhost:8080/logo.png// var express = require('express');// var app = express();// app.use(express.static(__dirname + '/public'));// app.listen(8080);//test 2: 打开一个指定的端口,浏览网页//浏览器输入http://localhost:8080// var express = require('express');// var app = express();// app.get('/', function (req, res) {//   res.send('Hello world!');// });// app.listen(8080);//test 3: 路由//浏览器输入http://localhost:8080/admin  客户端这样请求这个http链接的话,就会收到服务端的回应// var express = require('express');// var app = express();// var routes = require('./routes')(app); //这样就相当于扩展了app的方法啦,就是模块编程// app.listen(8080);//test 4: 模板//http://localhost:8080var express = require('express');var app = express(); var hbs = require('hbs');// 加载数据模块var blogEngine = require('./blog'); app.set('view engine', 'html');app.engine('html', hbs.__express);var bodyParser = require('body-parser')app.use(bodyParser()); app.get('/', function(req, res) {   res.render('index',{title:"最近文章", entries:blogEngine.getBlogEntries()});}); app.get('/about', function(req, res) {   // // res.render('about', {title:"自我介绍"});   // res.render('layout', {title:"自我介绍"});}); app.get('/article/:id', function(req, res) {   // var entry = blogEngine.getBlogEntry(req.params.id);   // // res.render('article',{title:entry.title, blog:entry});   // res.render('layout', {title:"内容", blog:"blog"});}); app.listen(8080);
2.blog.js

var entries = [{"id":1, "title":"第一篇", "body":"正文", "published":"6/2/2013"},{"id":2, "title":"第二篇", "body":"正文", "published":"6/3/2013"},{"id":3, "title":"第三篇", "body":"正文", "published":"6/4/2013"},{"id":4, "title":"第四篇", "body":"正文", "published":"6/5/2013"},{"id":5, "title":"第五篇", "body":"正文", "published":"6/10/2013"},{"id":6, "title":"第六篇", "body":"正文", "published":"6/12/2013"}];exports.getBlogEntries = function (){   return entries;} exports.getBlogEntry = function (id){   for(var i=0; i < entries.length; i++){      if(entries[i].id == id) return entries[i];   }}
3.blog.html

<!-- views/index.html文件 --><h1>文章列表</h1> {{#each entries}}   <p>      <a href="/article/{{id}}">{{title}}</a><br/>      Published: {{published}}   </p>{{/each}}<!-- 模板文件about.html。 --><!-- views/about.html文件 --><h1>自我介绍</h1> <p>正文</p><!-- 模板文件article.html。 --><!-- views/article.html文件 --><h1>{{blog.title}}</h1>Published: {{blog.published}} <p/> {{blog.body}}
4.package.json

{  "name": "hello-world",  "description": "hello world test app",  "version": "0.0.1",  "private": true,  "dependencies": {    "body-parser": "^1.18.2",    "express": "^4.16.2",    "hbs": "^4.0.1"  }}
5.rotes/index.js

//module.exports导出整个模块module.exports = function (app) {  app.get('/', function (req, res) {    res.send('Hello world');  });  app.get('/customer', function(req, res){    res.send('customer page');  });  app.get('/admin', function(req, res){    res.send('admin page');    // res.send(""+2);  });};

6.public/logo.png




总结:

可见,express编程,客户端http协议请求一个东西,服务端回应一个,一问一答的形式。





原创粉丝点击