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 🙏

© 2024 – Pkg Stats / Ryan Hefner

serberries

v1.4.7

Published

Serberries is a fast Nodejs live webserver that able to live refresh your script on the realtime.

Downloads

9

Readme

Written by Software License Tweet

Serberries

Serberries is a fast Nodejs live webserver that able to live refresh your script on the realtime.

This library was useful if you always updating your server script but tired of restarting your server every changes.

Install

To install this library, you can download from this repository or use

$ npm install serberries

Sample Usage

var Serberries = require('serberries');

var myserver = new Serberries({
    // path:__dirname+'/router', // (Deprecated)
    router:'router', // Directory path if you want to route http request
    modules:'modules', // Directory path for non router
    // All folder above have live reload on any script changes
    
    maxRequestSize:1e6, // Maximum size when receiving POST data
    allowedOrigins:['www.example.com'], // Allow CORS request
    public:"../public", // Set public folder for serving static assets
});

// Set public folder for serving static assets (Deprecated)
// myserver.setPublicFolder("../public");

// Set variable to be accessed from the router scopes
myserver.scopes.myWorld = "my world";

// Obtaining reference of http.createServer() to be used in Socket.io or other
// var app = myserver.server;

// Starting the server
myserver.start(8000);

Listen to events

myserver.on('error', function(errcode, msg, trace){
    console.error("Error code: "+errcode+' ('+msg+')');
    if(trace){
        console.error(trace.message);
        for (var i = 0; i < trace.stack.length; i++) {
            console.error("   at "+trace.stack[i]);
        }
    }
    console.error("");
});

myserver.on('loading', function(filename){
    console.log('Loading '+filename);
});

myserver.on('loaded', function(urlpath, type){
    console.log('URL to '+urlpath+' was '+type);
});

myserver.on('stop', function(){
    console.log("Server shutdown");
});

myserver.on('started', function(){
    console.log("Server was started");
});

myserver.on('removed', function(urlpath){
    console.log('URL to '+urlpath+' was removed');
});

myserver.on('httpstatus', function(code, callback){
    callback('Returning HTTP status '+code);
});

myserver.on('navigation', function(data){
    console.log("Navigation to '"+data.path+"'");
    console.log('  - '+data.headers['user-agent']);
});

Using router for handling request

// hello.js

// This will be initialized as an object on initialize
// you can keep your variable here and load it even
// the script reloaded
var scope = null;
var myWorld = '';

// Any variable declared not in `scope` variable
// will be reset to initial state
// so you can also live reload your other script
var dependency = require('./ext/hello.js');

function response(req, res, closeConnection){
    res.writeHead(200);

    // This will remain in `scope` variable even this script was reloaded
    // So if you have `setInterval`, you better save it on the scope
    scope.mySaveData = "I have some data that was saved";

    var output = dependency.hello() + " " + (req.get.who ? req.get.who : myWorld) + '!';

    //res.write(output);
    closeConnection(output);
}

// Minimal structure for router
module.exports = {
    // URL path
    path: '/hello', // URL path must be started with backslash
    // If you doesn't provide the backslash, it can't be accessed with URL
    // To use this router for all subpath '/hello#'

    // Response handler
    response:response,

    // Scope initialization after script loaded
    // scope:function(){}, [Deprecated]
    init:function(ref, scopes){
        scope = ref;
        myWorld = scopes.myWorld;

        if(ref.mySaveData)
            console.log(ref.mySaveData);

        // You can also access other scope with 'scopes'
        // 
        // this script filename is 'hello.js' so this
        // scope can be accessed from scopes['hello']
        // 
        // ref === scopes['hello']
        // scopes.myWorld === "my world"
    },

    // Optional if you want to destroy some data when script was reloaded
    destroy:function(){

    }
}

Define modules for the server

Actually you're free to build your module structure, but make sure that you saving your variables on the scope if you want to reuse it. Any variable that not referenced in another object will be removed when garbage collection process. Variable that referenced inside of setInterval or Event is not removed if it's still running.

// library-1.js
var scope = null;
module.exports = {
    // Optional if you want to access other scope
    // or saving data for current scope
    init:function(ref, allRef){
        // Running once this script was loaded
        scope = ref;
    },

    // Optional if you want to destroy some data when script was reloaded
    destroy:function(){

    }
}

Contribution

If you want to help in Serberries, please fork this project and edit on your repository, then make a pull request to here.

Keep the code simple and clear.

License

Serberries is under the MIT license.

But don't forget to put the a link to this repository.