1.1.0 • Published 2 years ago

als-server v1.1.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 years ago

Als-server

Server tested but if you have some errors, please write me to sh.mashkanta@gmail.com.

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
1.0.2

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

0.4.51

3 years ago

0.4.5

3 years ago

0.4.16

3 years ago

0.4.15

3 years ago

0.4.14

3 years ago

0.4.13

3 years ago

0.4.12

3 years ago

0.4.11

3 years ago

0.4.1

3 years ago

0.4.0

3 years ago

0.3.12

3 years ago

0.3.11

3 years ago

0.3.1

3 years ago

0.3.0

3 years ago

0.2.0

3 years ago

0.1.12

3 years ago

0.1.11

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago