简单的Nodejs Web服务器:serveMe

jopen 10年前

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.open-open.com/lib/view/home/1416552973258