onceio

1.2.3 • Public • Published

onceio

Lightweight modular node.js web framework

Install

npm install onceio

Usage

Create an app.js:

const onceio = require('onceio')
const app = onceio() //or onceio(options)

//Middleware
app.use('/', function(req, res) {
  console.log('middleware')
  req.filter.next()
})

//Handler
app.get('/', function(req, res) {
  res.send('hello world!')
})

run: node app.js and visit: http://127.0.0.1:8054

Options

options in: var app = onceio(optoins)

{
  //home folder of web
  home: './'

  //default port
  , port: 8054

  //default port of https
  , httpsPort:  8443
  , httpsKey:   ""
  , httpsCert:  ""

  //list files in directory
  , listDir: false
  //enable debug information output
  , debug: true
  //disable cache of template/include file (when disabled templates will be refreshed before restart)
  , templateCache: false
  //load from cache When size is smaller than fileCacheSize(css/js/images, 0 is disabled)
  , fileCacheSize: 0

  , gzipMinSize  : 1024
  , gzipFileType : [ '.css', '.js', '.txt', '.xml' ]

  //show errors to user(displayed in response)
  , showError: true

  //default pages, only one is supported
  , defaultPage: "index.html"
  , 404:         ""

  /*
  Session timeout, in milliseconds.
  */
  , sessionTimeout: 1440000

  //session file stored here
  , sessionDir    : ''

  //session domain
  , sessionDomain : ''
  , sessionKey    : '_wsid'
  , sessionLength : 36

  //tempary upload file stored here
  , uploadDir     :  os.tmpdir()
};

Examples

async/await

Middleware and handler support async / await:

const onceio  = require('onceio')
const util    = require('util')
const app     = onceio() //or onceio(options)


var normalFunc = function(delay, cb) {
  console.log('async delay', delay)
  setTimeout(cb, delay)
}

var asyncFunc = util.promisify(normalFunc)

app.use('/', async function(req, res) {
  await asyncFunc(1000)
  await asyncFunc(2000)

  console.log('middleware')

  req.filter.next()
})

app.get('/', async function(req, res) {

  await asyncFunc(1500)
  await asyncFunc(1000)

  console.log('handler')

  res.send('hello world!')
})

Result:

> node app.js
Http server running at home: ./ port: 8054
Filter app.js:13 /
async delay 1000
async delay 2000
middleware
Handle app.js:22 /
async delay 1500
async delay 1000
handler

Template

Use doT by default.

Modular

Register a new module

app.mod(module_name, static_folder_path)

Example:

const onceio = require('onceio')
const app = onceio() //or onceio(options)

//regist a module
app.mod('user', './user/web')

app.get('/user/signin', function(req, res) {
  /*
  send ./user/web/user.tmpl to the client
  */
  res.send('user.tmpl')
})

All the requests will be redirected:

/user/css/*.css => ./user/web/css/*.css
/user/js/*.js => ./user/web/js/*.js 
/user/*.tmpl => /user/web/*.tmpl

Include files

Include template files: <!--#include="file_path"-->

<!DOCTYPE html>
<html>
<head>
<!--#include="/site.meta.tmpl"-->
</head>

<body>

<!--#include="/user/header.tmpl"-->

<h1>Hellow world</h1>

<!--#include="/user/footer.tmpl"-->

</body>
</html>

Template override

You can override templates by using: res.template()

app.use('/', function(req, res) {
  res.model({
    title : 'Title override'
  })

  res.template({
      '/user/header.tmpl': '/form/header.tmpl'
    , '/user/footer.tmpl': '/form/footer.tmpl'
  })

  req.filter.next()
})

Document

OnceIO Help OnceIO 中文帮助

Readme

Keywords

none

Package Sidebar

Install

npm i onceio

Weekly Downloads

1

Version

1.2.3

License

none

Unpacked Size

574 kB

Total Files

98

Last publish

Collaborators

  • newghost