node 小结
2015-07-13 15:33
771 查看
express
node可以直接使用http module来实现web应用。
访问http://localhost:1984 即可
同时 web应用需要访问不同的url http也可以实现:
中间件: middleware
Express通过 app.use 注册middleware,middlewares相当于request的handlers,在处理request时,顺序执行每一个handler(function),handler业务完成后,通过调用next();,决定是否调用下一个handler。
路由: routing
使用express实现路由。
同时 express支持模板的view处理
使用jade引擎来渲染模板。
node中重要概念: 流
流的对接:
net(telnet)服务器返回的是连接对象 http返回的是request和response对象
node可以直接使用http module来实现web应用。
var http = require("http"); // 创建Server var app = http.createServer(function(request, response) { response.writeHead(200, { "Content-Type": "text/plain" }); response.end("Hello hyddd!\n"); }); // 启动Server app.listen(1984, "localhost");
访问http://localhost:1984 即可
同时 web应用需要访问不同的url http也可以实现:
// 加载所需模块 var http = require("http"); // 创建Server var app = http.createServer(function(request, response) { if(request.url == '/'){ response.writeHead(200, { "Content-Type": "text/plain" }); response.end("Home Page!\n"); } else if(request.url == '/about'){ response.writeHead(200, { "Content-Type": "text/plain" }); response.end("About Page!\n"); } else{ response.writeHead(404, { "Content-Type": "text/plain" }); response.end("404 Not Found!\n"); } }); // 启动Server app.listen(1984, "localhost");
中间件: middleware
// 加载所需模块 var express = require("express"); var http = require("http"); // 创建server var app = express(); // 增加一些middleware app.use(function(request, response, next) { console.log("step1, url:" + request.url); next(); }); app.use(function(request, response, next) { console.log("step2"); if(request.url == '/'){ response.writeHead(200, { "Content-Type": "text/plain" }); response.end("Main Page!\n"); } next(); }); app.use(function(request, response, next) { console.log("step2:"); console.log("step3"); if(request.url == '/about'){ response.writeHead(200, { "Content-Type": "text/plain" }); response.end("About Page!\n"); } }); // 启动server http.createServer(app).listen(1984);
Express通过 app.use 注册middleware,middlewares相当于request的handlers,在处理request时,顺序执行每一个handler(function),handler业务完成后,通过调用next();,决定是否调用下一个handler。
路由: routing
var express = require("express"); var http = require("http"); var app = express(); app.all("*", function(request, response, next) { console.log("step1"); next(); }); app.get("/", function(request, response) { response.end("Home Page!"); }); app.get("/about", function(request, response) { response.end("About Page!"); }); app.get("*", function(request, response) { response.end("404!"); }); http.createServer(app).listen(1984);
使用express实现路由。
同时 express支持模板的view处理
var express = require("express"); var app = express(); // 模板目录:./views app.set("views", __dirname + "/views"); // 使用jade引擎 app.set("view engine", "jade"); // 寻址views/index,提交jade渲染,并返回结果 app.get("/", function(request, response) { response.render("index", { message: "I'm hyddd" }); });
使用jade引擎来渲染模板。
node中重要概念: 流
流的对接:
require('fs').createReadStream('image.jpg').pipe(res)
net(telnet)服务器返回的是连接对象 http返回的是request和response对象
相关文章推荐
- Ruby中require、load、include、extend的区别介绍
- Windows系统下Node.js的简单入门教程
- 在Node.js应用中使用Redis的方法简介
- 在Ubuntu系统上安装Ghost博客平台的教程
- Node.js编程中客户端Session的使用详解
- 使用Raygun对Node.js应用进行错误处理的方法
- Node.js中require的工作原理浅析
- 在Mac OS下使用Node.js的简单教程
- 浅析Node在构建超媒体API中的作用
- 在Node.js中使用HTTP上传文件的方法
- Node调试工具JSHint的安装及配置教程
- Node.js实现简单聊天服务器
- 浅析Node.js中使用依赖注入的相关问题及解决方法
- php include和require的区别深入解析
- Javascript模块化编程(三)require.js的用法及功能介绍
- php 中include()与require()的对比
- node.js开发中使用Node Supervisor实现监测文件修改并自动重启应用
- 举例讲解Ruby中require的使用方法
- 充分发挥Node.js程序性能的一些方法介绍
- php中require和require_once的区别说明