npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

iot-server

v1.0.20

Published

This is a simple server for IoT devices. It stores data from devices and provides a simple API for getting data.

Downloads

86

Readme

iot-server

Description

This is a simple server for IoT devices. It stores data from devices and provides a simple API for getting data.

Installation

npm install iot-server

Usage

import startServer from "iot-server";

startServer(
    {
        serverPort: 8080,
        databaseType: 'mysql',
        database: {
            host: 'localhost',
            port: 3306,
            name: 'iot_database',
            user: 'root',
            password: '',
            table: 'iot_table'
        },
        rateLimit: 10000,
        fields: [
            {
                name: 'temperature',
                type: 'number',
            },
            {
                name: 'status',
                type: 'string',
            },
            {
                name: 'active',
                type: 'boolean',
            }
        ],
        secret: 'mysecretapikey123456789',
        payloadType: 'json',
        payloadLimit: 100,
    }
);

Configuration

The server can be configured with the following options:

  • serverPort - The port the server will listen on.
  • databaseType - The type of database to use. Currently only mysql is supported.
  • database - The database configuration. The following options are available:
    • host - The host of the database.
    • port - The port of the database.
    • name - The name of the database.
    • user - The user of the database.
    • password - The password of the database.
    • table - The name of the table to use.
  • rateLimit - The rate limit for the API in milliseconds.
  • fields - The fields that are allowed to be stored. The following options are available:
    • name - The name of the field.
    • type - The type of the field. The following types are available:
      • number - A number.
      • string - A string.
      • boolean - A boolean.
  • queries - Map of endpoints
    • {endpoint} - The name of the endpoint.
      • query - The query to execute.
      • serializer - The serializer to use. (Optional)
      • validators - The validators to use. (Optional)
        • {field name} - The function to validate specific field value
  • secret - The secret API key.
  • payloadType - The type of the payload. The following types are available:
    • json - A JSON payload.

API

The server provides the following API:

  • GET / - Returns the current server status.
  • GET /status?secret=[SECRET KEY]&beforeDate=[DATE]&afterDate=[DATE]&maxQuery=[NUMBER] - Returns the payload.
    • secret - The secret API key. (Optional if no secret is set)
    • beforeDate - The maximum date of the payload. (Optional)
    • afterDate - The minimum date of the payload. (Optional)
    • maxQuery - The maximum number of payloads to return. (Optional, default: 100)
  • GET /data?secret=[SECRET KEY]&<field name>=<field value>
    • secret - The secret API key. (Optional if no secret is set)
    • <field name> - The name of the field.
    • <field value> - The value of the field.

Response

The server will respond with the following status codes:

  • 200 - The request was successful.
  • 400 - The request was invalid.
  • 401 - The request was unauthorized. (Invalid secret key)
  • 429 - The request was rate limited.

The server will respond with the following payload:

  • status - The status of the request.
  • data - The data of the request. (For GET /status)
  • message - The message of the request. (Nullable)

The server will respond with the following status:

  • success - The request was successful.
  • fail - The request failed.
  • access-denied - The request was unauthorized.
  • rate-limited - The request was rate limited.

Example response:

{
  "status": "success",
  "data": [
    {
      "temperature": 20,
      "status": "working",
      "active": true,
      "date": "2020-01-01T00:00:00.000Z"
    },
    {
      "temperature": 20,
      "status": "working",
      "active": true,
      "date": "2020-01-01T00:00:00.000Z"
    }
  ],
  "message": "Successfully retrieved data."
}

Example

http://localhost:3000/
http://localhost:3000/status?beforeDate=2020-01-01T00:00:00.000Z&afterDate=2020-01-01T00:00:00.000Z&maxQuery=100
http://localhost:3000/data?temperature=20&status=working&active=true

Example Code

import startServer from "./iot-server.js";

startServer(
    {
        // Port yang digunakan untuk mengakses server
        serverPort: 80,
        // Konfigurasi database
        database: {
            type: 'mysql',
            host: 'localhost',
            port: 3306,
            name: 'iot',
            user: 'root',
            password: '',
            table: 'tb_cuaca',
            date_field: 'ts',
        },
        // Membatasi jumlah request yang dapat dilakukan dalam satu waktu
        // Jika melebihi batas, maka request akan ditolak
        // (dalam milidetik)
        rateLimit: 10000,
        allowAlterTable: false,
        // Berisi daftar fields yang ada pada tabel
        fields: [
            {
                // Nama field
                name: 'suhu',
                // Tipe data field
                type: 'float',
            },
            {
                name: 'humid',
                type: 'float',
            },
            {
                name: 'lux',
                type: 'float',
            },
        ],
        // Endpoint yang dapat diakses
        queries: {
            // Endpoint senddata (Contoh: http://localhost:8080/senddata?temp=20&humid=30&light=40)
            senddata: {
                query: 'INSERT INTO {table}(suhu, kelembapan, lux) VALUES ({temp}, {humid}, {light})',
                serializer: results => {
                    return results.length;
                },
                validators: {
                    suhu: value => {
                        return value >= 0 && value <= 100;
                    }
                }
            },
            // Endpoint getdata (Contoh: http://localhost:8080/getdata)
            getalldata: 'SELECT * FROM {table}',
            getlatestdata: 'SELECT * FROM {table} ORDER BY date DESC LIMIT 1',
            gethottestdate: 'SELECT {date} FROM {table} WHERE suhu = (SELECT MAX(suhu) FROM {table}) GROUP BY {date}',
        },
        secret: ip => {
            switch (ip) {
                // Jika diakses dari localhost, maka tidak perlu secret
                case '127.0.0.1':
                    return null;
                default:
                    // Jika diakses dari luar, maka secret adalah string berikut
                    return 'abcdefghijklmnopqrstuvwxyz';
            }
        },
        // Payload yang akan dikirimkan ke client
        payloadType: 'text',
    }
);