在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

简单的Nodejs Web服务器:serveMe

2016-12-20 13:17| 发布者: zhangjf| 查看: 642| 评论: 0

摘要: ServeMe是一个 DSL 用于利用nodejs创建简单的Web应用程序。ServeMe = require('serve-me')();ServeMe.start(3000);复制代码设置选项 其选项可以在ServeMe加载时设置,默认它将使用文件夹 "./public" 和文件 "i ...

ServeMe是一个 DSL 用于利用nodejs创建简单的Web应用程序。

  1. ServeMe = require('serve-me')();
  2. ServeMe.start(3000);
复制代码
设置选项

其选项可以在ServeMe加载时设置,默认它将使用文件夹 "./public" 和文件 "index.html".

  1. //Require the module
  2. ServeMe = require("serve-me");
  3. //Set the options
  4. ServeMe = ServeMe({
  5. debug: true,
  6. /**If debug mode is enabled, it will load each file again every http request, else the files will wait in cache.
  7. * Also prints more logs
  8. **/
  9. log: true,
  10. //(Optional) If log is enabled the server reports all the files served and more information.
  11. home: "mypage.html",
  12. //(Optional) home will change the html file served in "/" (by default: 'index.html')
  13. directory: "./www",
  14. //(Optional) directory will change the default public folder ('./public')
  15. error: {
  16. 404: "404.html",
  17. 500: "500.html"
  18. /**Error pages depending on the error code.
  19. * That specified files must exist in the 'public/error' folder.
  20. * Model: 'errorcode': "file.html"
  21. **/
  22. },
  23. //(Optional)
  24. secure: false,
  25. //Will use https when enabled.
  26. //ATENTION: A key and a certificate must be provided.
  27. //By default serve-me will use:
  28. key: "./keys/key.pem",
  29. cert: "./keys/cert.pem",
  30. });
  31. //Start the server
  32. 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:

  1. ServeMe.Routes.add("/hello", function(){
  2. return "Hello World!";
  3. });
复制代码

Delete a route example:

  1. ServeMe.Routes.reset("/hello");
复制代码
Events

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

  1. ServeMe.on("event_name", function(data){
  2. console.log("I am an event!");
  3. });
复制代码

"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:

  1. 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:

  1. ServeMe = ServeMe({
  2. debug: false,
  3. sessions:{
  4. enabled: true, //Enable sessions
  5. persistence: true, //if false disables the lifetime, and the session never ends (default true)
  6. lifetime: 86400, //Life of the session in seconds (default: 1 day)
  7. new_session_url: "/session/new", //Url selected to create sessions
  8. //new session petition will be created each visit
  9. }
  10. });
复制代码

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:

  1. var username = "bear",
  2. password = "drowssap";
  3. ServeMe.on("new_session", function(new_session){
  4. //new_session.data contains all the url arguments.
  5. //Login example:
  6. if( new_session.data.username == username &&
  7. new_session.data.password == password)
  8. {
  9. //if there are the correct credentials allow a new session creation, returning true.
  10. return true;
  11. }
  12. //else returning false.
  13. return false;
  14. });
复制代码

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

  1. /session/new?user=bear&password=drowssap
复制代码

will give us that session.data:

  1. >{
  2. > user: "bear",
  3. > password: "drowssap"
  4. >}
复制代码

项目主页:http://www.open-open.com/lib/view/home/1416552973258

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-8-19 05:35

Copyright 2015-2025 djqfx

返回顶部