-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
94 lines (85 loc) · 2.61 KB
/
server.js
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
var connect = require('connect');
var HttpDispatcher = require('httpdispatcher');
var dispatcher = new HttpDispatcher();
var fs = require('fs');
var util = require('util');
var path = require('path');
var serveStatic = require('serve-static')
var port = 8080;
// 承载页
dispatcher.onGet('/flow', function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/html', 'Cache-Control': 'no-cache' });
fs.readFile('./flow2-2.html', null, function(error, data){
if(error) {
res.writeHead(404);
res.write('File not fount!');
} else {
res.write(data);
}
res.end();
});
});
dispatcher.onGet('/svg-arrow', function(req, res) {
res.writeHead(200, { 'Content-Type': 'text/html', 'Cache-Control': 'no-cache' });
fs.readFile('./svg-arrow.html', null, function(error, data){
if(error) {
res.writeHead(404);
res.write('File not fount!');
} else {
res.write(data);
}
res.end();
});
});
(new connect())
// 输出请求连接
.use(function logger(req, res, next) {
console.log('%s %s', req.method, req.url);
next();
})
// 返回资源文件
.use(function staticFiles(req, res, next) {
var filePath = '.' + req.url;
var extname = path.extname(filePath);
var contentType = '';
var isStaticFile = false;
switch (extname) {
case '.js':
contentType = 'text/javascript';
isStaticFile = true;
break;
case '.css':
contentType = 'text/css';
isStaticFile = true;
break;
case '.png':
case '.jpg':
case '.gif':
contentType = 'image/' + extname.replace('\.', '');
isStaticFile = true;
// 提出images 里面的style
filePath = filePath.replace(/styles\//, '');
break;
default: break;
}
if(isStaticFile) {
fs.readFile(filePath, function(error, content) {
if (error) {
res.writeHead(404);
res.write('File not fount!');
}
else {
res.writeHead(200, { 'Content-Type': contentType });
res.end(content, 'utf-8');
}
res.end();
});
} else {
next();
}
})
// 接口路由
.use(function interfaces(req, res, next) {
dispatcher.dispatch(req, res);
})
.listen(port)