Node.js 学习笔记 七

Node.js Express框架

Express 简介

Express 是一个简洁而灵活的 node.js Web应用框架, 提供了一系列强大特性帮助你创建各种 Web 应用,和丰富的 HTTP 工具。

使用 Express 可以快速地搭建一个完整功能的网站。

Express 框架核心特性:

  • 可以设置中间件来响应 HTTP 请求。
  • 定义了路由表用于执行不同的 HTTP 请求动作。
  • 可以通过向模板传递参数来动态渲染 HTML 页面。

安装Express

安装 Express 并将其保存到依赖列表中:

npm i express -S

以上命令会将 Express 框架安装在当前目录的 node_modules 目录中, node_modules 目录下会自动创建 express 目录。以下几个重要的模块是需要与 express 框架一起安装的:

  • body-parser - node.js 中间件,用于处理 JSON, Raw, Text 和 URL 编码的数据。
  • cookie-parser - 这就是一个解析Cookie的工具。通过req.cookies可以取到传过来的cookie,并把它们转成对象。
  • multer - node.js 中间件,用于处理 enctype=“multipart/form-data”(设置表单的MIME编码)的表单数据。

安装命令:

npm i body-parser cookie-parser multer -S

第一个Express框架实例

接下来我们使用 Express 框架来输出 “Hello World”。

创建express_demo.js文件,内容如下:

const express = require('express');
const app = express();

app.get('/',function(req,res) {
	res.send('Hello World');
})

const server = app.listen(8090,function() {
	const host = server.address().address;
	const port = server.address().port;
	console.log('应用实例:访问地址为http://%s:%s',host,port);
})

运行 node express_demo.js,访问 http://localhost:8090/

[外链图片转存失败(img-9C9mRBHu-1563882787217)(readme_files/1.jpg)]

请求和响应

Express 应用使用回调函数的参数: request 和 response 对象来处理请求和响应的数据。

app.get('/', function (req, res) {
   // --
})

request 和 response 对象的具体介绍:

Request 对象 - request 对象表示 HTTP 请求,包含了请求查询字符串,参数,内容,HTTP 头部等属性。常见属性有:

  1. req.app:当callback为外部文件时,用req.app访问express的实例
  2. req.baseUrl:获取路由当前安装的URL路径
  3. req.body / req.cookies:获得「请求主体」/ Cookies
  4. req.fresh / req.stale:判断请求是否还「新鲜」
  5. req.hostname / req.ip:获取主机名和IP地址
  6. req.originalUrl:获取原始请求URL
  7. req.params:获取路由的parameters
  8. req.path:获取请求路径
  9. req.protocol:获取协议类型
  10. req.query:获取URL的查询参数串
  11. req.route:获取当前匹配的路由
  12. req.subdomains:获取子域名
  13. req.accepts():检查可接受的请求的文档类型
  14. req.acceptsCharsets / req.acceptsEncodings / req.acceptsLanguages:返回指定字符集的第一个可接受字符编码
  15. req.get():获取指定的HTTP请求头
  16. req.is():判断请求头Content-Type的MIME类型

Response 对象 - response 对象表示 HTTP 响应,即在接收到请求时向客户端发送的 HTTP 响应数据。常见属性有:

  1. res.app:同req.app一样
  2. res.append():追加指定HTTP头
  3. res.set()在res.append()后将重置之前设置的头
  4. res.cookie(name,value [,option]):设置Cookie
  5. opition: domain / expires / httpOnly / maxAge / path / secure / signed
  6. res.clearCookie():清除Cookie
  7. res.download():传送指定路径的文件
  8. res.get():返回指定的HTTP头
  9. res.json():传送JSON响应
  10. res.jsonp():传送JSONP响应
  11. res.location():只设置响应的Location HTTP头,不设置状态码或者close response
  12. res.redirect():设置响应的Location HTTP头,并且设置状态码302
  13. res.render(view,[locals],callback):渲染一个view,同时向callback传递渲染后的字符串,如果在渲染过程中有错误发生next(err)将会被自动调用。callback将会被传入一个可能发生的错误以及渲染后的页面,这样就不会自动输出了。
  14. res.send():传送HTTP响应
  15. res.sendFile(path [,options] [,fn]):传送指定路径的文件 -会自动根据文件extension设定Content-Type
  16. res.set():设置HTTP头,传入object可以一次设置多个头
  17. res.status():设置HTTP状态码
  18. res.type():设置Content-Type的MIME类型

路由

我们已经了解了 HTTP 请求的基本应用,而路由决定了由谁(指定脚本)去响应客户端请求。
实例:

const express = require('express');
const app = express();

// 主页输出Hello World
app.get('/',function(req,res) {
	console.log('主页get请求');
	res.send('Hello World!');
})

// POST请求
app.post('/',function(req,res) {
	console.log('主页 POST 请求');
	res.send('hello POST');
})

// del_user 页面响应
app.get('/del_user',function(req,res) {
	console.log('/del_user 响应 DELETE请求');
	res.send('删除页面');
})

// /list_user 页面请求
app.get('/list_user',function(req,res) {
	console.log('/list_user get请求');
	res.send('用户列表界面');
})

// 	对页面 abcd , abxcd , ab123cd 等get请求作出响应
app.get('/ab*cd',function(req,res) {
	console.log('/ab*cd get请求');
	res.send('正则匹配');
})

const server = app.listen(8090,function() {
	const host = server.address().address;
	const port = server.address().port;
	console.log('应用实例:访问地址为 http://%s:%s',host,port);
})

启动服务,根据地址请求页面:

[外链图片转存失败(img-QWzj9tbI-1563882787221)(readme_files/2.jpg)]

[外链图片转存失败(img-o8c9smML-1563882787226)(readme_files/3.jpg)]

[外链图片转存失败(img-tcWReBzX-1563882787227)(readme_files/4.jpg)]

[外链图片转存失败(img-NfPQLqXq-1563882787242)(readme_files/5.jpg)]

静态文件

Express 提供了内置的中间件 express.static 来设置静态文件如:图片, CSS, JavaScript 等。

你可以使用 express.static 中间件来设置静态文件路径。例如,如果你将图片, CSS, JavaScript 文件放在 public 目录下,你可以这么写:

app.use('/public', express.static('public'));

实例:
在public目录下创建images目录,然后放入图片 likeFlower.png

const express = require('express');
const app = express();

app.use('/public',express.static('public'));
app.get('/',function(req,res) {
	res.send('Hello World');
})
const server = app.listen(8090,function() {
	const host = server.address().address;
	const port = server.address().port;
	console.log('应用实例:访问地址为 http://%s:%s',host,port);
})

启动后访问 http://localhost:8090/public/images/likeFlower.png ,可以看到图片。

GET方法

以下实例演示了在表单中通过 GET 方法提交两个参数,我们可以使用 server.js 文件内的 process_get 路由器来处理输入:
实例:
创建index.html文件:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>GET方法</title>
	</head>
	<body>
		<form action="http://localhost:8090/process_get" method="GET">
			First Name: <input type="text" name="first_name">  <br>
			Last Name: <input type="text" name="last_name">
			<input type="submit" value="Submit">
		</form>
	</body>
</html>

创建server.js文件:

const express = require('express');
const app = express();

app.use('/public',express.static('public'));
app.get('/index.html',function(req,res) {
	res.sendFile(__dirname+'/index.html');
})
app.get('/process_get',function(req,res) {
	// 输出json格式
	const response = {
		'first_name': req.query.first_name,
		'last_name': req.query.last_name
	}
	console.log(response);
	res.end(JSON.stringify(response));
})
const server = app.listen(8090,function() {
	const host = server.address().address;
	const port = server.address().port;
	console.log('应用实例:访问地址为 http://%s:%s',host,port);
})

然后访问 http://localhost:8090/index.html ,如图所示:
[外链图片转存失败(img-mB2V4TZq-1563882787244)(readme_files/1.gif)]

POST 方法

以下实例演示了在表单中通过 POST 方法提交两个参数,我们可以使用 server.js 文件内的 process_post 路由器来处理输入:
创建index.html文件:

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title>GET方法</title>
	</head>
	<body>
		<form action="http://localhost:8090/process_post" method="POST">
			First Name: <input type="text" name="first_name">  <br>
			Last Name: <input type="text" name="last_name">
			<input type="submit" value="Submit">
		</form>
	</body>
</html>

创建server.js文件:

const express = require('express');
const app = express();
const bodyParser = require('body-parser');

// 创建 application/x-www-form-urlencoded 编码解析
const urlencodedParser = bodyParser.urlencoded({extended: false});

app.use('/public',express.static('public'));
app.get('/index.html',function(req,res) {
	res.sendFile(__dirname+'/index.html');
})
app.post('/process_post',urlencodedParser,function(req,res) {
	// 输出json格式
	const response = {
		'first_name': req.body.first_name,
		'last_name': req.body.last_name
	}
	console.log(response);
	res.end(JSON.stringify(response));
})
const server = app.listen(8090,function() {
	const host = server.address().address;
	const port = server.address().port;
	console.log('应用实例:访问地址为 http://%s:%s',host,port);
})

访问 http://localhost:8090/index.html ,如图所示:
[外链图片转存失败(img-Hfm5Qfu1-1563882787245)(readme_files/2.gif)]

文件上传

以下我们创建一个用于上传文件的表单,使用 POST 方法,表单 enctype 属性设置为 multipart/form-data。
创建index.html文件:

<html>
	<head>
		<title>文件上传表单</title>
	</head>
	<body>
		<h3>文件上传:</h3>
		选择一个文件上传: <br />
		<form action="/file_upload" method="post" enctype="multipart/form-data">
			<input type="file" name="image" size="50" />
			<br />
			<input type="submit" value="上传文件" />
		</form>
	</body>
</html>

创建server.js文件:

const express = require('express');
const fs = require('fs');
const app = express();

const bodyParser = require('body-parser');
const multer = require('multer');

app.use('/public',express.static('public'));
app.use(bodyParser.urlencoded({extended: false}));
app.use(multer({dest: '/tmp/'}).array('image'));
app.get('/index.html',function(req,res) {
	res.sendFile(__dirname+'/index.html');
})

app.post('/file_upload',function(req,res) {
	console.log(req.files[0]);	// 上传的文件信息
	let des_file = __dirname + "/" + req.files[0].originalname;
	fs.readFile(req.files[0].path,function(err,data) {
		fs.writeFile(des_file,data,function(err) {
			let response = {};
			if(err) {
				console.log(err);
			}else {
				response = {
					message: 'file upload successfully',
					filename: req.files[0].originalname
				}
			}
			console.log(response);
			res.end(JSON.stringify(response));
		})
	})
})
const server = app.listen(8090,function() {
	const host = server.address().address;
	const port = server.address().port;
	console.log('应用实例:访问地址为 http://%s:%s',host,port);
})

访问 http://localhost:8090/index.html ,如图:
[外链图片转存失败(img-AC0bH1Kx-1563882787246)(readme_files/3.gif)]

Cookie管理

我们可以使用中间件向 Node.js 服务器发送 cookie 信息,以下代码输出了客户端发送的 cookie 信息:

const express = require('express');
const util = require('util');
const cookieParser = require('cookie-parser');

const app = express();
app.use(cookieParser());

app.get('/',function(req,res) {
	console.log('Cookies : '+ util.inspect(req.cookies));
})

app.listen(8090);

输出如下:

Cookies : { _ga: 'GA1.1.957650906.1560677112' }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值