簡單的Nodejs Web服務器:serveMe

jopen 10年前發布 | 18K 次閱讀 serveMe Node.js 開發

ServeMe是一個 DSL 用于利用nodejs創建簡單的Web應用程序。

ServeMe = require('serve-me')();

ServeMe.start(3000);

設置選項

其選項可以在ServeMe加載時設置,默認它將使用文件夾 "./public" 和文件 "index.html".

//Require the module
ServeMe = require("serve-me");

//Set the options
ServeMe = ServeMe({
    debug: true,
        /**If debug mode is enabled, it will load each file again every http request, else the files will wait in cache.
         * Also prints more logs
        **/

    log: true,
        //(Optional) If log is enabled the server reports all the files served and more information.

    home: "mypage.html",
        //(Optional) home will change the html file served in "/" (by default: 'index.html')

    directory: "./www",
        //(Optional) directory will change the default public folder ('./public')

    error: {
        404: "404.html",
        500: "500.html"
        /**Error pages depending on the error code.
         * That specified files must exist in the 'public/error' folder.
         *     Model: 'errorcode': "file.html"
        **/
    },

    //(Optional)
    secure: false,
        //Will use https when enabled.
        //ATENTION: A key and a certificate must be provided.
    //By default serve-me will use:
    key: "./keys/key.pem",
    cert: "./keys/cert.pem",
});

//Start the server
ServeMe.start(3000);//3000 is the port. Of course you can change it.

Routes

To add specific actions to url paths ServeMe includes Routes.

Create a route example:

ServeMe.Routes.add("/hello", function(){
    return "Hello World!";
});

Delete a route example:

ServeMe.Routes.reset("/hello");

Events

To add actions to specific events yo can use the ServeMe Events.

ServeMe.on("event_name", function(data){
    console.log("I am an event!");
});

"event_name" is the name required to select de action wanted.

These are the available events for now:

  • "http_request": Will be called each http connection.
  • "new_session": Will be called when a new session can be created.
  • "end_session": Will be called when an existing session lifetime ends.
  • "session": Will be called when an existing session connects.
  • "error": Will be called when an error appears.

If you want to create your own event, you can activate it with:

ServeMe.call("event_name");

Sessions

The sessions have been implemented to facilitate the creation of user sessions or similar tasks, mainly using cookies.

First of all we need to enable sessions in the serveme options:

ServeMe = ServeMe({
    debug: false,

    sessions:{
        enabled: true, //Enable sessions
        persistence: true, //if false disables the lifetime, and the session never ends (default true)
        lifetime: 86400, //Life of the session in seconds (default: 1 day)
        new_session_url: "/session/new", //Url selected to create sessions 
        //new session petition will be created each visit
    }
});

Then "session/new" will reqistry each visitor in a new session. But one more step is needed. To alow the customization of the session registry you can use the "new_session" event like this:

var username = "bear",
    password = "drowssap";
ServeMe.on("new_session", function(new_session){
    //new_session.data contains all the url arguments.
    //Login example:
    if( new_session.data.username == username &&
        new_session.data.password == password)
    {
        //if there are the correct credentials allow a new session creation, returning true.
        return true;
    }
    //else returning false.
    return false;
});

session.data contains all the url arguments, for example a session request like

/session/new?user=bear&password=drowssap

will give us that session.data:

>{
>    user: "bear",
>    password: "drowssap"
>}

項目主頁:http://www.baiduhome.net/lib/view/home/1416552973258

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!