Server.js

Server.js
Build Status
Build Status

Write server code in frontend JavaScript files, and more.

The purpose of this project is to unify all JavaScript code in frontend. With the support of server.js solution, frontend developers are now able to embed backend codes in fontend files.

Thanks to node.js, JavaScript developers can now write one language for both frontend and backend. But due to different host environments, the developers come from frontend world usually need a lot of time to learn about backend development skills before getting their first web app running. And as long as the web app grows, although frontend and backend codes are written in the same language, they are not organized well - actually they have to be isolated. This will bring much effort for further development, which usually involves code changes on both ends and also their communicating interfaces.

Some existing solutions move the fontend codes to backend. For example, Meteor allows developers to combine codes for both ends in the same backend js files. Server.js does the similar thing, but on the opposite direction, which allows embedding server code in frontend js files. Since most functions of a web app is triggered from the frontend - the end user's web browser, I believe it's more reasonable to move code to the front.

The ideal use case is that the developer only need play attention to frontend files and keep backend untouched, and everything just go well.

Imagine that you are designing the login page for your web app. There's an input for username and another one for password and at the bottom a Login button. Once the user clicks the Login button, your onclick event handler is triggered, where you get the entered username and password. With server.js, you can now directly check database whith this information right in your onclick event handler. How wonderful it is!

How to install

mkdir your_project
cd your_project
npm install serverjs --save

How to start

Under your project directory, create file app.js and write your app code (as easy as one line of code)

// app.js
(new require('serverjs')).start();

And then start you app with node.js.

node app.js

Now the server is started at port 3000. Open your web browser and navigate to http://localhost:3000 you'll see a default Hello, World web page while the same message is printed in backend console.

Directory structure

You may have noticed that in your project directory, a sub-directory named public is automatically created on your first run, which is the root folder for your website. The default index.html and index.js files are also created for a start (home page). You can also put your other html files, js files and all assets under this directory. Once you put an html file as well as a js file with the same file name, the js file will be automatically embedded in that html file within a script tag. This follows the principle of convention over configuration, which you'll see in many places in server.js solution.

Hot code reload

Inspired by Meteor, hot code reload is very useful during development process. Server.js also introduces this technology, which means if any file under public directory has been changed, the web browser will be automatically reloaded to reflect the change. You don't need to manually reload the page for every update.

How to code

Once you place the string statement 'server code' on the first line of a function (or the second line, right after 'use strict'), it'll become a backend function, and you can write your backend code directly in it. To invoke this backend function from frontend, besides all original parameters for this function, just put an Error-First Callback at the end, as you usually do in most async function calls.

Following are several quick demos. Try to put them in your index.js file and see the results.

Hello, world (default)

function hello() {
  'server code';
  console.log('Hello, world!');
}

hello();

Hello, world (anonymous)

(function() {
  'server code';
  console.log('Hello, world!');
})();

Node version

function getNodeVersion(callback) {
  'server code';
  callback(null, process.version);
}

getNodeVersion(function(err, ver) {
  alert('Node version: ' + ver);
});

File system

var readdir = function(callback) {
  'server code';
  var fs = require('fs');
  fs.readdir(__dirname, callback);
}

readdir(function(err, files) {
  if (err) {
    alert('Error!');
    return;
  }
  alert('Files:\n' + files.join('\n'));
});

Easy routing

Basides the magical inline RPC calls, server.js also introduces an easy way of routing system.

Template engine

Handlebars

The main function

TBD

Shared code

TBD

RESTful API

TBD

Sessions

TBD

Development mode

Development mode is enabled by default. You can change this setting to production mode by:

  • environment variable
  • initialization option

Behavior differences between development mode and production mode are listed as below.

TABLE

Security considerations

TBD

Inside server.js

The chapter contains some advanced details about server.js.

Initialization options

TBD

About 'server code'

TBD

RPC

Client and server communicate via a WebSocket connection (or the ajax fallback).

Hot code reload

TBD

More samples

see Server.js Samples

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子赠堵,更是在濱河造成了極大的恐慌小渊,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,602評(píng)論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件茫叭,死亡現(xiàn)場(chǎng)離奇詭異粤铭,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)杂靶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,442評(píng)論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)酱鸭,“玉大人吗垮,你說(shuō)我怎么就攤上這事“妓瑁” “怎么了烁登?”我有些...
    開(kāi)封第一講書人閱讀 152,878評(píng)論 0 344
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)蔚舀。 經(jīng)常有香客問(wèn)我饵沧,道長(zhǎng),這世上最難降的妖魔是什么赌躺? 我笑而不...
    開(kāi)封第一講書人閱讀 55,306評(píng)論 1 279
  • 正文 為了忘掉前任狼牺,我火速辦了婚禮,結(jié)果婚禮上礼患,老公的妹妹穿的比我還像新娘是钥。我一直安慰自己,他們只是感情好缅叠,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,330評(píng)論 5 373
  • 文/花漫 我一把揭開(kāi)白布悄泥。 她就那樣靜靜地躺著,像睡著了一般肤粱。 火紅的嫁衣襯著肌膚如雪弹囚。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書人閱讀 49,071評(píng)論 1 285
  • 那天领曼,我揣著相機(jī)與錄音鸥鹉,去河邊找鬼。 笑死庶骄,一個(gè)胖子當(dāng)著我的面吹牛宋舷,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播瓢姻,決...
    沈念sama閱讀 38,382評(píng)論 3 400
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼祝蝠,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起绎狭,我...
    開(kāi)封第一講書人閱讀 37,006評(píng)論 0 259
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤细溅,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后儡嘶,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體喇聊,經(jīng)...
    沈念sama閱讀 43,512評(píng)論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,965評(píng)論 2 325
  • 正文 我和宋清朗相戀三年蹦狂,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了誓篱。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 38,094評(píng)論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡凯楔,死狀恐怖窜骄,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情摆屯,我是刑警寧澤邻遏,帶...
    沈念sama閱讀 33,732評(píng)論 4 323
  • 正文 年R本政府宣布,位于F島的核電站虐骑,受9級(jí)特大地震影響准验,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜廷没,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,283評(píng)論 3 307
  • 文/蒙蒙 一糊饱、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧颠黎,春花似錦济似、人聲如沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書人閱讀 30,286評(píng)論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)。三九已至唉铜,卻和暖如春台舱,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背潭流。 一陣腳步聲響...
    開(kāi)封第一講書人閱讀 31,512評(píng)論 1 262
  • 我被黑心中介騙來(lái)泰國(guó)打工竞惋, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人灰嫉。 一個(gè)月前我還...
    沈念sama閱讀 45,536評(píng)論 2 354
  • 正文 我出身青樓拆宛,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親讼撒。 傳聞我的和親對(duì)象是個(gè)殘疾皇子浑厚,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,828評(píng)論 2 345

推薦閱讀更多精彩內(nèi)容