-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
99 lines (88 loc) · 2.83 KB
/
app.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
95
96
97
98
99
//--------------------------------------------------------
// npm install express mockjs supervisor glob --save-dev
// npm run Server
//--------------------------------------------------------
const express = require('express');
const os = require('os');
const fs = require('fs');
const path = require('path');
const glob = require("glob");
const URL = require('url');
// 自定义模块用来回去本机的ip地址
const getIPAdress = require('./Utils/getIPAdress.js');
const app = express();
const applicationRoot = __dirname.replace(/\\/g, "/");
const ipAddress = process.env.OPENSHIFT_NODEJS_IP || getIPAdress();
const port = process.env.OPENSHIFT_NODEJS_PORT || 3000;
console.log(applicationRoot + " " + ipAddress + " " + port);
//
/* app.configure(function() {
app.use(express.logger());
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
}); */
//
app.use('/lottery', require('./model/lottery'));
//允许客户端进行跨域访问
app.all('/api/*', function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
res.header("Access-Control-Allow-Headers", "X-Requested-With");
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
});
//获取mock 数据源
const commentData = require('./detail/comment.js');
app.get('/api/:star', function (req, res) {
res.send(commentData.data.filter(item => {
return item.star === parseInt(req.params['star']);
}));
});
//404
app.get('/404', function (req, res) {
// res.render('404.html', {
// title: 'No Found'
// })
res.status(404);
res.redirect('/404.html');
});
// 不满足url进行重定向
app.get('*', function (req, res) {
var urlObj = URL.parse(req.url);
console.log(urlObj.path);
var urlPath = urlObj.path;
var index = urlPath.indexOf('?');
if (index > -1) {
urlPath = urlPath.substring(0, urlPath.indexOf('?'));
}
var filePath = path.join(applicationRoot, urlPath);
if (fs.existsSync(filePath)) {
var contentText = fs.readFileSync(filePath, 'utf-8');
fs.readFile(filePath, 'utf8', function (err, data) {
if (err) throw err;
res.send(data);
// var data = JSON.parse(data)
// if (data[req.params.apiName]) {
// res.json(data[req.params.apiName])
// } else {
// res.send(data)
// }
});
} else {
res.status(404);
res.redirect('/404.html')
//
/* var data = fs.readFileSync(fileName, 'utf8');
res.writeHead(200, {
'Content-Type': 'application/json'
});
res.write(data);
res.end(); */
//
}
});
//监听服务端口 输出配置信息
const server = app.listen(port, function () {
const host = server.address().address;
const port = server.address().port;
console.log('Mork Server address http://' + ipAddress + ':' + port + '/api');
});