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

als-server

v1.1.0

Published

node server with routes

Readme

Als-server

Server tested but if you have some errors, please write me to [email protected].

als-server is node.js server with routes, data, csrf, session, headers and coockies.

Installation and preparations

npm i als-server

Create app.js or file with any other name you want, on your root folder. Inside app.js, you need to add your settings and to run the server. For development, it can be realy short. Here the short version:

let Route = require('als-server')
let {join} = require('path')

Route.dev = true

Route.get('/',(req,res,route) => 
/*html*/`<div>Hello from test</div>`)

Route.server(3000)

But you can customize folders and other settings, like so:

let Route = require('als-server')
let {join} = require('path')

/* Session and coockies settings */
   Route.sessionPath = join(__dirname,'sessions') // path for session files
   Route.secret = 'some key' // secret key for session
   Route.add = 'add key' // addition key to create hash for session and csrf token
   Route.duration = 168*3600*1000 // time for coockies and sessions.  by default week(168*3600*1000)
   Route.app = 'app' // Your app name and name for coockies for session

/* App folders */
   Route.controllersPath = join(__dirname,'controllers')
   Route.public = join(__dirname,'public') // set public path

/* App settings */
   Route.dev = true // not cleaning sessions and cleaning cache for required files

/* Error pages (has to be string can be html for showing errors) */
   Route.e404 = '404: Not found'
   Route.e202 = '202: Not Allowed'
   Route.e500 = '500: Server Error'

/* Routes */
   Route.get('/',(req,res,route) => 
   /*html*/`<div>Hello from test</div>`)

/* Running server */
   Route.server(3000)

You can just copy paste the code above and change your settings.

Routes system

You have 2 ways to create routes:

  1. By creating route in your app.js with included module or function (first priority)
  2. By creating module in controllers folder and folowing name convension (second priority)

Route in your app.js

You can define get and post routes in your app.js. To do that, you need to call to get or post methods, and give them 2 parameters:

  1. Route
  2. function or array path to module which return the result for response.end.

Here example:

Route.get('/',[__dirname,'index'])
Route.post('/post',[__dirname,'post'])
Route.get('/test',(req,res,route) => /*html*/`<div>Hello from test</div>`)

Module in controllers folder

On your app.js you have setted controllersPath. When we create controller, route created automaticly. Except site's root /, which will has to be index.js, all the rest will called by name of routes.

Each file, has to be with exported class with methods for each route. Let's see some examples.

__

Route http://your-site.com/

// controllersPath/index.js
module.exports = class Index {
   constructor() {}

   GET() {return /*html*/`<div>HEllo world</div>`}
   POST() {return Index.route.redirect()}
   PUT() {return Index.route.redirect()}
   DELETE() {return Index.route.redirect()}
}

The exaple above, has 4 routes. Then it will be get request on root path, GET method will return html. Then it POST/PUT/DELETE requests - the same analogy. The method name has to be in upperCase.

Let's create anothe route for /test.

Route http://your-site.com/test

// controllersPath/test.js
module.exports = class Test {
   constructor() {}
   GET() {return /*html*/`<div>HEllo from test</div>`}
}

Let's test some more complicated route:

Route http://your-site.com/test/some

// controllersPath/test.js
module.exports = class Test {
   constructor() {}
   GET() {return /*html*/`<div>HEllo from test</div>`}
   someGET() {return /*html*/`<div>HEllo from test some</div>`}
}

And even more complicated route with parameters:

Route http://your-site.com/test/some/5/8

// controllersPath/test.js
module.exports = class Test {
   constructor() {
      let parts = Test.route.parts
      this.user = parts[0] // 5
      this.parameter = parts[1] // 8
   }
   GET() {return /*html*/`<div>HEllo from test</div>`}
   someGET() {return /*html*/`<div>
      HEllo user ${this.user}. 
      You have some ${this.parameter}
   </div>`}
}

As shown on example above, the route works like this:

_

/controllerName/methodName/paramater/parameter/...

_

Session and coockies

You can simply update session and coockies values, just by editing values in objects route.session and route.coockies.

Here example how it works:

// controllersPath/index.js
module.exports = class Index {
   constructor() {}
   GET() {
      let session = Index.route.session
      console.log(session.csrf) // return csrf token it self
      session.user = 'some user'
      session.some = 'some'
      delete session.some

      let coockies = Index.route.coockies
      coockies.test = 'some test'

      return /*html*/`<div>HEllo world</div>
   `}
}

How it works? Then request recieved, Route parse coockies to js object and get session as js object too. Before response.end, Route update session and coockies and make it same as session and coockies objects.

Csrf token and POST/PUT/DELETE methods

In case the route has PUT/DELETE method or POST/GET methods with data, csrf token will be checked. If csrf not given or it is wrong, client will get Route.p202 message with code p202.

In order not to get 202 error, you need to include csrf token in your reqeust. Let's see how it works.

// controllersPath/test.js
module.exports = class Test {
   constructor() {}
   GET() {return /*html*/`
      <div>Hello from some</div>
      <form method="post" action="/test">
      ${Test.route.csrf}
      <input type="text" name="test">
      <button type="submit">Submit</button>
      </form>`
   }

   POST() {
      console.log(Test.route.data)
      return Test.route.redirect()
   }
}

As you can see in example above, Test.route.csrf in an input tag with csrf token as value.

Now let's send a put request:

// controllersPath/test.js
module.exports = class Test {
   constructor() {}
   GET() {return /*html*/`
      <div>Hello from some</div>
      <form method="post" action="/test">
      ${Test.route.csrf}
      ${Test.route.put}
      <input type="text" name="test">
      <button type="submit">Submit</button>
      </form>`
   }

   PUT() {
      console.log(Test.route.data)
      return Test.route.redirect()
   }
}

As you can see on the example above, to send put reqeust, you need to add Test.route.csrf which is hidden input field with _method="put". The same thing is with delete request. Just add Test.route.delete and method will be DELETE.

Instruments

route.getProtocol() // return http or https
route.redirect(url) // if url undefined, redirecting back, else redirects to relative path