Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nodejs(一) 路由 #6

Open
Alexandermclean opened this issue Mar 16, 2019 · 0 comments
Open

nodejs(一) 路由 #6

Alexandermclean opened this issue Mar 16, 2019 · 0 comments
Labels
Node.js Node.js

Comments

@Alexandermclean
Copy link
Owner

Alexandermclean commented Mar 16, 2019

/*
	Apache请求数据库的代码
*/
res = db.query('SELECT * from some_table')
res.output()

/*
	Nodejs请求数据库的代码
*/
db.query('SELECT * from some_table', function(res){
	res.output()
})

由于项目开发的深入,为了配合设备层的配置下发,后台开始着手core层的接口,也就是说不完全针对页面显示的数据接口;这种情况下需要前台对接口进行路由分发,对于后台给出的接口改构和包装,达到页面显示需要的数据结构的接口,这次用的是nodejs的express框架,在应用到项目前,这里算是学习笔记和感想。

一个基本的express应用的结构:

var express = require('express') // 安装node的时候回自动安装express
var app = express()

app.get('/', (req,res) => { // req(请求)和res(响应)与Node提供的对象完全一致
	res.send('监听3030端口进入的所有get请求')		
})
var server = app.listen(3030, function{
	var host = server.address().address
  	var port = server.address().port
  	console.log('Example app listening at http://%s:%s', host, port)
})

1.路由

路由是指如何定义应用的端点(URIs)以及如何响应客户端的请求。
路由是由一个 URI、HTTP 请求(GET、POST等)和若干个句柄组成,它的结构如下: app.METHOD(path, [callback...], callback), app 是 express 对象的一个实例, METHOD 是一个 HTTP 请求方法, path 是服务器上的路径, callback 是当路由匹配时要执行的函数。

// 对于上面的4种请求的句柄改用app.route()定义链式句柄
app.route('/public')
	.get((req,res) => {
		...
	})
	.post((req,res) => {
		...
	})
	.put((req,res) => {
		...
	});
// 监听来自/public的所有请求
app.all('/public', function(req,res,next){
	...
	next();
})
// 路由匹配
// 匹配 butterfly、dragonfly,不匹配 butterflyman、dragonfly man等
app.get(/.*fly$/, function(req, res) {
  	res.send('/.*fly$/');
});

express.Router

调用express()方法创建的Application(app)内部都创建了一个Router,大部分对 Application 的操作实际上都被重定向到了这个内部的Router上而已。而Application所做的,只不过是在这个Router的基础上添加了一些额外的便捷 API 而已。

var express = require('express');
var router = express.Router();

// 该路由使用的中间件
router.use(function timeLog(req, res, next) {
  console.log('Time: ', Date.now());
  next();
});
// 定义网站主页的路由
router.get('/', function(req, res) {
  res.send('Birds home page');
});
// 定义 about 页面的路由
router.get('/about', function(req, res) {
  res.send('About public');
});

module.exports = router;

// 在应用中加载路由模块
var pub = require('js文件路径');
...
app.use('/public', pub);
// 应用即可处理发自 /public 和 /public/about 的请求,并且调用为该路由指定的 timeLog 中间件

2.基本路由和静态文件挂载

常见的4个基本http请求:

// 对网站首页的访问返回 "Hello World!" 字样
app.get('/', function (req, res) {
  res.send('Hello World!');
});

// 网站首页接受 POST 请求
app.post('/', function (req, res) {
  res.send('Got a POST request');
});

// /user 节点接受 PUT 请求
app.put('/user', function (req, res) {
  res.send('Got a PUT request at /user');
});

// /user 节点接受 DELETE 请求
app.delete('/user', function (req, res) {
  res.send('Got a DELETE request at /user');
});

利用express托管静态文件(express.static中间件)

app.use(express.static('public'))

// 通过http://localhost:3030/image/xx.png访问
// 多个目录按照添加顺序查找

app.use('/public', express.static('public'))
// 存放虚拟目录,通过指定的挂载路径访问:http://localhost:3030/public/image/xx.png
@Alexandermclean Alexandermclean changed the title nodejs-路由分发 nodejs-路由 Mar 16, 2019
@Alexandermclean Alexandermclean changed the title nodejs-路由 nodejs(1)-路由 Mar 16, 2019
@Alexandermclean Alexandermclean changed the title nodejs(1)-路由 nodejs(一)-路由 Mar 16, 2019
@Alexandermclean Alexandermclean changed the title nodejs(一)-路由 nodejs(一)路由 Mar 16, 2019
@Alexandermclean Alexandermclean changed the title nodejs(一)路由 nodejs(一) 路由 Mar 16, 2019
@Alexandermclean Alexandermclean added the Node.js Node.js label Mar 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Node.js Node.js
Projects
None yet
Development

No branches or pull requests

1 participant