Code Monkey home page Code Monkey logo

mock-server's Introduction

Hi there, I'm Sivaraman - aka Siva ๐Ÿ‘‹

I'm a Creative Sr.Frontend Engineer and a Stack Explorer!!

I enjoy learning programming languages and frameworks like React, Angular, NextJS etc.. I like neat and clean code with a simpler logic. I also love Data structure and Algorithms (beginner). I'm a pet lover and I have a pet dog named Scooby ๐Ÿ•โ€๐Ÿฆบ. My Hobbies are playing Chess, watching Movies and series and learning something new. I like to do things that makes me feel WOW. I'm also a fan of Marvel movies ๐Ÿ˜….

About me

  • ๐Ÿ”ญ Iโ€™m currently working at a position Sr.Software Engineer at Ford !
  • ๐ŸŒฑ Iโ€™m currently learning everything ๐Ÿคฃ
  • โค๏ธ I love writing TypeScript, and building fun experiments on type-level
  • ๐Ÿ‘ฏ Iโ€™m looking to collaborate with other content creators
  • ๐Ÿฅ… 2022 Goals: Contribute more to Open Source projects
  • โšก Fun fact: I love to draw and play chess

Skills and Experience

  • React
  • Angular
  • NodeJs
  • TypeScript
  • HTML, CSS, JS
  • VS Code Extensions
  • NPM Packages

GitHub Stats:

My Stats


Connect with me:

Email Profile LinkedIn Instagram

mock-server's People

Contributors

r35007 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

mock-server's Issues

Not working "_AdvancedSearch" middlewares

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"]
},

Rewriters not working as expected

@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"
}

Startup Inconsistent cli vs vscode

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)

  1. CLI using mock-server.json
  2. CLI using params
  3. 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
Screenshot 2022-11-04 at 1 43 54 PM

Homepage:

  • no database
  • no rewriters
    Screenshot 2022-11-04 at 1 52 44 PM

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
Screenshot 2022-11-04 at 1 49 13 PM

Homepage: All databases AND rewriters are shown

  • /products - works as expected
  • /api/products - works as expected
  • /api/products/45345454 - 404s
    Screenshot 2022-11-04 at 1 50 26 PM

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

Screenshot 2022-11-04 at 1 53 47 PM

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.