r35007 / mock-server Goto Github PK
View Code? Open in Web Editor NEWCustomise Your Own Local Mock Server
License: MIT License
Customise Your Own Local Mock Server
License: MIT License
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
// 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"
}
Notice here: PORT does not persist to 4000. Also no logs in my server.js are seen in the console
Homepage:
$ 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
Homepage:
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.
ex) not working
"/users": {
"_config": true,
"description": "The given fetch path will be relative to the root path given in config",
"fetch": ".mock/data/users.json",
"middlewares": ["_AdvancedSearch"]
},
ex) working
"/users": {
"_config": true,
"description": "The given fetch path will be relative to the root path given in config",
"mock": [
{
"userId": 1,
"id": 1,
"title": "Lorem ipsum dolor sit.",
"body": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Est, dolorem."
},
{
"userId": 1,
"id": 2,
"title": "Lorem ipsum dolor sit.",
"body": "Lorem ipsum dolor sit amet consectetur adipisicing elit. Est, dolorem."
}
],
"middlewares": ["_AdvancedSearch"]
},
@R35007 - Hey thanks so much for the work here. Really love everything you are building especially the work on the homepage addition compared to other mocking tools.
Any chance you can shed some light on why my rewriters aren't showing up? I have experienced them showing sometimes and not others when I restart the server. Thoughts?
// server.js
const { MockServer } = require("@r35007/mock-server");
const db = require("./data/db");
const quiet = false; // Set to true to suppress console logs
const log = true; // Set to true to see logs. If quiet is false the we cant see the setter logs.
const port = 3000; // Set Port to 0 to pick a random available port. default: 3000
const host = "localhost"; // Set empty string to set your Local Ip Address
const config = { root: __dirname, port, host, quiet };
const mockServer = MockServer.Create(config);
const app = mockServer.app;
// Sets global injectors, middlewares, store and rewriters
mockServer.setData(
{
injectors: "./injectors.json",
middlewares: "./middlewares.js",
store: "./store.json",
rewriters: "./rewriters.json",
},
{ 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();
// 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 });
// 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(port, host); // can also set port and host here
// rewriters.json
{
"/api/*": "/$1"
}
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.