1 Star 0 Fork 43

NoC/mockcat

forked from KingWTD/mockcat 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
server.js 2.28 KB
一键复制 编辑 原始数据 按行查看 历史
KingWTD 提交于 2022-08-25 12:50 . 添加请求路径为/的数据MOCK支持
/**
* Mockcat Server
* @author Wangtd
*/
const express = require('express');
const favicon = require('serve-favicon');
const session = require('express-session');
const bodyParser = require('body-parser');
const cookieParser = require("cookie-parser");
const config = require('./config.js');
const util = require('./lib/util.js');
const mocked = require('./lib/mocked.js');
const router = require('./lib/router.js');
const security = require('./lib/security.js');
const git = require('./lib/git.js');
const proxy = require('./lib/proxy.js');
var app = express();
var basePath = (config.debug ? 'app' : 'dist');
// favicon
app.use(favicon(util.paths(basePath + '/images/favicon.ico')));
// cookie & session
app.use(cookieParser());
app.use(session(config.sessionConfig));
// security
util.writeFileSync(config.workDir + '/users.json', '[]');
app.use(/^\/(?!app|mock\/auth)\/?.+$/,
security.checkPermission([/^(?!\/mock\/).+/, '/mock/file/', '/mock/test/']));
// homepage
app.use('/', function(request, response, next) {
var url = request.url;
if (url == '/') {
var homepage = basePath + '/index.html';
response.writeHead(200, { 'content-type': 'text/html' });
response.end(util.readFileSync(util.paths(homepage)));
} else if (url == '/app/') {
response.redirect('/');
} else {
next();
}
});
// app
app.use('/app', express.static(util.paths(basePath)));
// parameter
app.use(bodyParser.urlencoded({ limit: '5mb', extended: false }));
app.use(bodyParser.json({ type: 'application/json', limit: '5mb' }));
// router
app.use(router);
// blogs
util.mkdirsSync(util.path(config.blogDir, 'images'));
util.mkdirsSync(util.path(config.blogDir, 'articles'));
util.writeFileSync(config.blogDir + '/blogs.json', JSON.stringify({
categories: [],
articles: []
}, null, 2));
app.use('/blog', express.static(config.blogDir));
// mock
util.mkdirsSync(config.mockDir);
app.use('/', mocked.mock(config.mockDir));
// init git and task
git.initRepository();
git.startCommitTask();
// mock proxy
setTimeout(proxy.loadAllServers, 500);
// start http server
app.listen(config.port, '0.0.0.0', function(error) {
if (error) {
console.error(error);
} else {
console.log('Mockcat server is running at http://localhost:%d/', config.port);
}
});
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
NodeJS
1
https://gitee.com/No_C/mockcat.git
[email protected]:No_C/mockcat.git
No_C
mockcat
mockcat
master

搜索帮助