Overview
As I have been working on getting setup I am running into a few inconsistencies I will outline here.
$ node -v
v16.17.0
$ npm -v
8.15.0
Example startup tests (see below)
- CLI using mock-server.json
- CLI using params
- Running via VSCode
server.js and mock-server.json files
// server.js
const { MockServer } = require("@r35007/mock-server");
const db = require("./data/db");
const config = require("./mock-server.json");
console.log("-----------------config");
console.log(config);
console.log("-----------------config");
const log = config.logger; // Set to true to see logs. If quiet is false the we cant see the setter logs.
const config = {
root: __dirname,
port: config.port,
host: config.host,
quiet: config.quiet,
};
const mockServer = MockServer.Create(config);
const app = mockServer.app;
// Sets global injectors, middlewares and store
mockServer.setData(
{
injectors: config.injectors,
middlewares: config.middlewares,
store: config.store,
},
{ log }
); // pass mockServer instance to use it in middleware.js method
// Make sure to use this at first, before all the resources
const rewriter = mockServer.rewriter(config.rewriters); // Give Rewriters path or object here
// Returns the default middlewares
const defaultsMiddlewares = mockServer.defaults();
// add your authorization logic here
const isAuthorized = (_req) => true;
// Custom Middleware
app.use((req, res, next) => {
if (isAuthorized(req)) return next(); // continue to Mock Server router
res.sendStatus(401);
});
// Creates resources and returns the express router
const resources = mockServer.resources(db, { log });
console.log(resources);
// Create the Mock Server Home Page
const homePage = mockServer.homePage({ log });
app.use(rewriter);
app.use(defaultsMiddlewares);
app.use(resources.router);
app.use(homePage);
app.use(mockServer.pageNotFound); // Middleware to return `Page Not Found` as response if the route doesn't match
app.use(mockServer.errorHandler); // Default Error Handler
// mockServer.startServer();
mockServer.startServer(config.port, config.host); // can also set port and host here
// mock-server.json
{
"port": 3002,
"host": "localhost",
"base": "",
"id": "id",
"dbMode": "mock",
"static": "public",
"reverse": false,
"logger": true,
"noCors": false,
"noGzip": false,
"readOnly": true,
"bodyParser": true,
"cookieParser": true,
"quiet": false,
"db": "./data/db.js",
"rewriters": "./rewriters.json",
"injectors": "./injectors.json",
"store": "./store.json",
"middlewares": "./middlewares.js"
}
Mock-Server startup Examples
1. CLI (mock-server.json)
Notice here: PORT does not persist to 4000. Also no logs in my server.js are seen in the console
Homepage:
- no database
- no rewriters
2. CLI w/ params
$ mock-server --watch ./data/db.js --rw=./rewriters.json --st=./store.json --md=middlewares.js --P=4000 -l=true -q=false
Notice here: No logs again, but port 4000 is shown
Homepage: All databases AND rewriters are shown
- /products - works as expected
- /api/products - works as expected
- /api/products/45345454 - 404s
3. VSCode "Mock It" Button
- App starts at PORT 3001??
- Where would I see the logs if I could?
Homepage:
- Shows db and rewriters
- Same issue with routes as example 2
I think this outlines the startup pretty well. As you can see I moved all configs in server.js
to load from the mock-server.json
but its not following that during startup for whatever reason. I would be able to debug further but unable to get any logging to work even when I hard code quiet=false
and log=true
.
Thanks again. Hope this helps others.