core.server
Preface
The core.server
object provides methods allowing to control the internal HTTP server.
Use only the API methods described in this docs. Use other methods under your own risk, and take into account that they may change in minor versions without considering it as a breaking change.
API
restart()
core.server.restart()
: Restart the HTTP server.
Example
await core.server.restart();
addRouter()
core.server.addRouter(path, expressRouter)
: Add a custom express router to the server. Custom routers are added just before the middleware that serves the mock routes, so if a custom router path matches with a route path, the first one will have priority.
path
(String): Api path for the custom routerexpressRouter
(Express Router): Instance of anExpress
router.
Example
const express = require("express");
const customRouter = express.Router();
core.server.addRouter("/custom-path", customRouter);
removeRouter()
core.server.removeRouter(path, expressRouter)
: Remove a custom Express router previously added with the core.server.addRouter
method.
path
(String): Api path of the custom router to be removed.expressRouter
(Express Router): Instance of the express router to be removed.
Example
const express = require("express");
const customRouter = express.Router();
core.server.addRouter("/custom-path", customRouter);
core.server.removeRouter("/custom-path", customRouter);
protocol
core.server.protocol
: Returns currently used protocol ("http" or "https") depending if HTTPS is enabled or not.
Example
console.log(core.server.protocol) // -> http
url
core.server.url
: Returns current server base url. If the host equals to 0.0.0.0
(its default value), it is replaced by localhost
Example
console.log(core.server.url) // -> http://localhost:3100