-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouter.js
More file actions
40 lines (37 loc) · 1.28 KB
/
router.js
File metadata and controls
40 lines (37 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
/**
* Created by wtt on 2016/11/20.
*/
"use strict";
const config = require('./config'); // 配置文件模块
const fs = require('fs'); // 文件处理模块
const url = require('url'); // 解析处理url模块
const path = require('path'); // 路径处理模块
const handlers = require('./handlers'); //
module.exports = (req, res) => {
const urlObj = url.parse(req.url, true); // 解析请求url
const pathname = urlObj.pathname; // 得到请求地址
const query = urlObj.query; // 得到查询字符串
// 给req挂载一个属性
req.query = query;
// 处理静态资源请求
if (pathname.startsWith('/node_modules/') || pathname.startsWith('/public/') || pathname.startsWith('/uploads/')) {
fs.readFile(`.${pathname}`, (err, data)=> {
if (err) {
throw err;
}
res.end(data);
})
}
// 根据不同的请求路径做不同的响应
else if (pathname === '/') {
handlers.showIndex(req, res);
} else if (pathname === '/album') {
handlers.showAlbums(req, res);
} else if (pathname === '/getAlbums') {
handlers.getAlbums(req, res);
} else if(pathname === '/add'){
handlers.addAlbum(req,res);
} else {
handlers.handle404(req, res);
}
}