🚀 Big News:Socket Has Acquired Secure Annex.Learn More →
Socket
Book a DemoSign in
Socket

snapsrv4u

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

snapsrv4u

Light-weight package for developers that need a reacting server in a blink of an eye

latest
Source
npmnpm
Version
1.1.0
Version published
Weekly downloads
5
Maintainers
1
Weekly downloads
 
Created
Source

🚀 Mock API Server

A powerful and flexible mock API server that automatically generates realistic data based on your specifications using Faker.js, it also optionally integrates with a file-based CSV database "snap4db" to simulate persistent data and accordingly reacts as an API.

📦 Installation

npm install snapsrv4u

🚀 Quick start without a database

Supports GET requests only

const { MockApiServer } = require('snapsrv4u');

const server = new MockApiServer(4517); // default value

server.addRoute('/api/users', {
  method: 'GET',
  count: 10, // number of objects created
  properties: { // description of all the objects (created with faker.js)
    id: { type: 'id', zeros: 5 },
    name: { type: 'name' },
    email: { type: 'email' }
  }
});

server.start(); // starting the server

Supported types for Faker.js

CategoryTypes
Numbersnumber, float, id, uuid
Stringsstring, sentence, paragraph
Datesdate, past, future, timestamp
Personalfirstname, lastname, fullname, username, email, avatar, phone, gender, jobTitle, bio
Locationlatitude, longitude, city, country, address, zipcode
Interneturl, ipv4, password, useragent
Companycompany, department, catchPhrase
Commerceproduct, price, color
Booleanboolean
Customfaker with method and options (e.g., faker: { method: 'music.genre' })

📝 Creating a server with database

GET POST and DELETE requests

const server = new MockApiServer(port); // port defaults to 4517

Adding POST routes

// POST request
server.addRoute('/sign-up', {
    method: 'POST', // required
    collection: 'users', // required
    properties: {
        name: { type: 'name' }, // describes the the property
        email: { type: 'email', unique: true } // can be a unique property
    }
});

Adding DELETE routes

Gets data from body, params or query

// POST request
server.addRoute('/delete-from-known-collection/:id', {
    method: 'DELETE',
    collection: 'users'
});

server.addRoute('/delete-from-unknown-collection/:id', {
    method: 'DELETE'
});

Adding GET routes

Gets data from body, params or query

// POST request
server.addRoute('/get-from-known-collection/:id', {
    method: 'GET',
    collection: 'users'
});

server.addRoute('/get-from-unknown-collection/:id', {
    method: 'GET'
});

Config Options

  • method: HTTP method ('GET', 'POST' or 'DELETE')
  • count: Number of items to generate (1 for single object, >1 for array). will only work with quick start
  • properties: Object describing the data structure

📚 Supported Data Types

TypeDescriptionOptions
numberRandom numbermin, max
stringRandom stringmin, max (length)
booleanRandom true/false-
dateRandom datefrom, to
nameRandom full name-
emailRandom email address-
uuidRandom UUID-
idNumeric ID with paddingzeros (padding length)

Or anything else from faker.js

đź”§ Configuration Options

Each property can have these configurations:

  • type: (Required) Data type to generate
  • min: Minimum value/length
  • max: Maximum value/length
  • zeros: Number of digits for IDs
  • from: Start date for date ranges
  • to: End date for date ranges

Future features

  • PUT requests
  • Adding the option to send data automatically (on timer)
  • Creating a log for incoming requests
  • Customizable response (function)

Keywords

mocker

FAQs

Package last updated on 16 May 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts