Common Scenarios

In this chapter, we'll discuss some common scenarios which could occur in your web applications:

File Upload

By default, coServ would upload files to the COIMOTION API services. Using the COIMOTION API services, you automatically get a storage space to store the upload files. However, you can still using coServ to receive and store (or process) the upload files. Here we'll show you how.

First of all, you have to designate a block (or URL) to handle the file uploading and let coServ know you'll handle the file upload yourself instead of leveraging the COIMOTION API services. This can be done by the following step:

  1. Create a new block to handle the file upload. You may use the design console to do it. Assume the newly created block is '/myUpload'.
  2. In the siteURI.json file ($website_root/$website_codename/themes/$website_theme/siteURI.json), find the "/myUpload" entry (if you haven't create the block yet, you'll have to add the entry instead of finding it). Add an "upload" property and set its value to true. Below is an sample:
"/myUpload": {
  "id": "no",
  "upload": true
}

With the setting done, we can now work on the block itself. The file upload block which we're going to implement will be a block without a view and controller. The block only need a backend model to receive upload files. The block server model can be something like:

exports.execute = function(ctx, inData, cb)  {
    var  files = inData.files,
         fileObj = files.myFile,
         fileDest = fileObj.path;

    // fileDest point to where the upload file is temporarily stored
    ...
}

As you can see, upload files will be kept in the inData.files property. Depending on what label you use to upload the files, if it's 'myFile' then the file object can be accessed by inData.files.myFile.

The Ajax call

The last thing is to initiate file uploading from browsers. Below are sample codes:

ctrl.importSite = function(files)  {
    // prepare the form data
    var  formData = new FormData();
    formData.append('siteFile', files[0]);

    // send the request
    var  xhr = new XMLHttpRequest();
    xhr.onload = function()  {
        if (xhr.status === 200)
            alert('Ok');
        else
            alert('Failed to uplod the website.');
    };

    xhr.open('POST', '/myUpload.wsj');
    xhr.send( formData );
}

Access Protection

Sometimes you want to protected web pages from unauthorized access. coServ offers a very simple scheme to help you achieve that.

You can protect the whole website and allow only few pages (such as the login page) to be accessed without user login. You can also protect just some particular pages of a website and leave the rest open. To protect the whole website, you can add the "isProtected" property to your website described in the sites.json as shown below:

{
  "127.0.0.1": {
        "title": "My Website",
        ...
        "isProtected": true,
        "login": "url_of_the_login_page"
    }
}

When an user makes an unauthroized access to a protected page, he/she will be directed to the login page or the home page or the '/index' page (in the order specified). Both the login or the home page can be specified in the sites.json file (this only works for release 0.11.0 or later).

To protected a specific page from unauthorized access, you can add the "isProtected" property to the block (page) URL specified in the siteURI.json file. Below is an example:

{
  ...
  "myPage": {
    "id": "no",
    "isProtected": true
  }
}

If the "isProtected" property is not specified on a block (page), coServ will resort to the settings on the website. For example, if a website is protected while the "isProtected" property is not specified for a block b, the block b is protected. However, if the "isProtected" property is set for a block, the protection settings on its website will not be referenced.

results matching ""

    No results matching ""