Skip to content

Commit 064b52d

Browse files
committed
修改静态文件服务配置
1 parent c63a67c commit 064b52d

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

index.d.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
export declare class KsMock {
2-
public option: mock.options;
3-
constructor(option: mock.options);
4-
public apply(compiler: any): void;
5-
public server(option?: mock.options): void;
6-
}
71
export namespace mock {
82

93
interface anyObject<T=any> {
@@ -29,6 +23,8 @@ export namespace mock {
2923
crossDomain?: boolean;
3024
// 端口号
3125
port?: number;
26+
// 静态文件目录
27+
static?: string | ((jsonServer: Object, server: Object) => void);
3228
// https证书配置
3329
https?: {
3430
key: string;
@@ -64,4 +60,11 @@ export namespace mock {
6460
}
6561
) => undefined | object | string | Function;
6662
}
63+
export declare class KsMock {
64+
public option: mock.options;
65+
constructor(option: mock.options);
66+
public apply(compiler: any): void;
67+
public server(option?: mock.options): void;
68+
}
69+
6770
export default KsMock

src/server.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,20 @@ import { mock } from '../';
1010
const server = jsonServer.create()
1111
// 路径从根目录开始?
1212
const router = jsonServer.router(path.resolve(process.cwd(), 'db.json'))
13-
const middlewares = jsonServer.defaults({
14-
static: path.resolve(__dirname, './public')
15-
})
1613

17-
server.use(middlewares)
1814
const createServer = (option: mock.anyObject, callback?: Function) => {
1915
let config = option.https
2016
config = /^(boolean|number)$/.test(typeof config) ? config && {} : config
2117
let currentServer
2218
if (config instanceof Object) {
19+
if (typeof config.static === 'function') {
20+
config.static(jsonServer, server)
21+
} else {
22+
const middlewares = jsonServer.defaults({
23+
static: typeof config.static === 'string' ? config.static : path.resolve(process.cwd(), './public')
24+
})
25+
server.use(middlewares)
26+
}
2327
if (typeof config.key !== 'string' || typeof config.cert !== 'string' || config.key.length + config.cert.length === 0) {
2428
config.key = fs.readFileSync(path.join(__dirname, 'ssl/key.pem'))
2529
config.cert = fs.readFileSync(path.join(__dirname, 'ssl/cert.pem'))

0 commit comments

Comments
 (0)