New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

onecode

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

onecode

Write the same code on client and server. At last.

  • 0.2.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3
decreased by-25%
Maintainers
1
Weekly downloads
 
Created
Source

OneCode Build Status

Description

This web framework tries to get the best from 2 worlds: traditional and single-page websites.

  • Full html code of the page is served to the browser - good for web crawlers, social networks and older browsers.
  • After the page is loaded and shown, the website is transparently switched to single-page mode: no more full-page refreshes, no more html is transmitted over the network, only client-rendered data.
  • The same Controller/View codebase is used both on client AND server, so you don't need to worry to synchronize them. (That's what Node.js promised from the very beginning after all!)
  • Very fast - no browser emulators on server, pure Node.js. Html serving speed is similar to the usual Node.js/Express setups.
  • Built upon widely known technologies - Node.js, Browserify, Backbone.

Installation

$ npm install onecode

Framework

OneCode builds on the single-page website (rich web client) architecture consisting of 2 major parts:

  1. Database and business logic accessible through a well-defined REST API. No source code of this part is visible to client.
  2. View/interaction logic and templates that are sent to the browser. This is javascript and html templates and full source is visible to client.

OneCode helps running the second part both on client and on the server effectively:

  • Unifies access to REST API from both client and server ($.ajax and Backbone.sync).
  • Unifies routing (Backbone.Router/History).
  • Views explicitly separate the html templating (server+client) and dynamic behaviors (client only).
  • After html is generated on server, the page state/data is transparently moved to browser, where it is recreated (all js objects) and dynamic behaviors attached to existing DOM nodes without any html re-rendering or issuing additional requests to the REST API.

Limitations:

  • As the code is executed both on client and server, no globals are allowed (this is a good thing anyways).

Example

We will use CoffeeScript as a safer and more expressive alternative, but this can be written in raw javascript as well.

# This is a main.coffee file, standard Express boilerplate.
express = require 'express'
browserify = require 'browserify'
onecode = require 'onecode'

app = express.createServer()

# Make ./client_script/index.coffee (and all dependent modules) accessible to the client as '/main.js'
app.use browserify mount: 'main.js', entry: './client_script/index.coffee'

# Make the same file work in server environment and prepare to serve requests
# according to Backbone routes.
app.use onecode require('./client_script/index.coffee')

app.listen 3000
# ./client_script/index.coffee: works both on client and server.

OneCode = require 'onecode/client'
OneCode.exportToGlobals() # This makes 'Backbone', '_', 'OneCode', '$' accessible globally.

# The app has everything to render on server and continue working with it on client.
# It is created on every server request. 
class exports.App extends OneCode.App
    start: ->
        
        

(Sample apps: http://addyosmani.github.com/todomvc/)

License: MIT

Keywords

FAQs

Package last updated on 23 May 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc