-
Notifications
You must be signed in to change notification settings - Fork 14
/
server.js
executable file
·61 lines (54 loc) · 1.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
#!/usr/bin/env node
import cors from "cors";
import express from "express";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";
import fixtureServerMiddleware from "../index.js";
import globToFixtures from "../lib/glob-to-fixtures.js ";
import DEFAULTS from "../lib/defaults.js";
// NOW_URL: support deployment to now.sh: https://zeit.co/docs/features/env-and-secrets
const defaultFixtureUrl =
process.env.NOW_URL || process.env.FIXTURES_URL || DEFAULTS.fixturesUrl;
const { argv } = yargs(hideBin(process.argv))
.options({
port: {
type: "number",
default: parseInt(process.env.PORT || DEFAULTS.port, 10),
},
"fixtures-url": {
type: "string",
default: defaultFixtureUrl,
},
"log-level": {
type: "string",
describe: "Set logging level for Express",
default: process.env.LOG_LEVEL || DEFAULTS.logLevel,
},
ttl: {
type: "number",
describe: "Expiration time for loaded fixtures in ms",
default: parseInt(process.env.TTL || DEFAULTS.ttl, 10),
},
fixtures: {
type: "string",
description: "glob path for JSON fixture files created by nock",
default: process.env.FIXTURES || DEFAULTS.fixturesGlob,
},
})
.help();
const app = express();
app.use(cors());
app.get("/ping", (request, response) => {
response.json({ ok: true });
});
app.use(
fixtureServerMiddleware({
port: argv.port,
fixturesUrl: argv["fixtures-url"],
logLevel: argv["log-level"],
ttl: argv.ttl,
fixtures: globToFixtures(argv.fixtures),
}),
);
app.listen(argv.port);
console.log(`🌐 http://localhost:${argv.port}`);