nodeframe

A modular web application framework based-on popular node technologies (express, swig and etc.).

Nodeframe

A modular web application framework based-on popular node technologies (express, swig and etc.).

MIT

0.2.0


  1. Write a "package.json" file as below.

     {      
         "name": "Project Name",  
         "version": "0.0.1",  
         "private": true,  
         "scripts": {  
             "start": "node app-server.js"  
         },  
         "dependencies": {  
             "nodeframe": "*"  
         }  
     }  
    
  2. Run "npm install" under the same directory with "package.json".

  3. Write a "app-server.js" file as below.

     require('nodeframe').start();
    
  4. Run "node app-server.js".

  5. Try visit http://localhost:3000

  1. Create a module directory "hello_nodeframe" under the "web_modules" directory which is automatically created by previous operations.

  2. Write a "bootstrap.js" as below under the newly created directory.

     module.exports = {
    
         version: '0.0.1', // the version of the new module
    
         routes: { // routes settings
    
             '/': {
                 rule: { // router type, currently supports rule/mvc/rest
                     rules: { // required for rule based router
                         '/': { // sub-path
                             get: 'home.index' // http method and controller action
                         }
                     }
                 }
             }
    
         }
     };
    
  3. Create a controllers directory "controllers" under the "hello_nodeframe" directory.

  4. Write the controller file "home.js" as below under the "controllers" directory.

     module.exports = {
         index: function (router) { // controller action
    
             router.appendViewData({
                 title: 'Hello nodeframe!'
             });
    
             // to render "index" view template, modify router.view to render other template
             router.renderResponse();
         }
     };
    
  5. Create a views directory "views" under the "hello_nodeframe" directory.

  6. Write the view template "home_index.swig" as below under the "views" directory.

     {{ title }}
    
  7. Update the "etc/development.js" file that generated by 1) as below to enable the new module.

    module.exports = {
    
        modules: {
            hello_nodeframe: {
                route: '/hello' // can be whatever path you want, e.g. /test, /module1
            }
        }
    };
    
  8. Run "node app-server.js" again.

  9. Try visit http://localhost:3000/hello

  1. Create a module directory "hello_wechat" under the "web_modules" directory.

  2. Write a "bootstrap.js" as below under the "hello_wechat" directory.

     module.exports = {
    
         version: '0.0.1', // the version of the new module
    
         routes: { // routes settings
    
             '/': {
                 rule: { // router type, currently supports rule/mvc/rest
                     
                     rules: { // required for rule based router
                         '/': 'wechat.service'
                     }
                 }
             }
    
         },
         
         middlewares: {
             'wechat': {
                 token: 'your_wechat_token_here'
             }
         }
     };
    
  3. Create a controllers directory "controllers" under the "hello_wechat" directory.

  4. Write the controller file "wechat.js" as below under the "controllers" directory.

     module.exports = {
         service: function (router) {
             var message = router.req.weixin;
     
             if (message.MsgType === 'event') {
     
                 if (message.Event === 'subscribe') {
                     return router.res.reply('Welcome to subscribe!');
                 }
     
                 return router.res.reply('I am fine.');
             }
     
             if (message.MsgType === 'text') {
                 return router.res.reply(message.Content);
             }
     
             router.res.reply('Hi');
         }
     };
    
  5. Update the "etc/development.js" file that generated by 1) as below to enable the new module.

    module.exports = {
    
        wechat: {},
    
        modules: {
            hello_nodeframe: {
                route: '/hello' // can be whatever path you want, e.g. /test, /module1
            },
            
            hello_wechat: {
                route: '/wechat' // can be whatever path you want, e.g. /test, /module1
            }
        }
    };
    
  6. Run "node app-server.js" again.

  7. Try visit http://localhost:3000/wechat, and you should see "invalid signature".

  8. Now you can use this web module as the back-end application to serve wechat request. (PS: Wechat requires the service to listen on 80 port.)