Help.sjs

From PlainOldWebserver

Jump to: navigation, search

Home FAQ Help InfiniLink POW - Plain Old Webserver

   * NAME
   * SYNOPSIS
   * DESCRIPTION
         o How do I manage POW?
         o Where are my files?
         o Why can't I access my web server from the outside?
         o Can I create dynamic content?
         o How do I read a file?
         o How do I write a file?
         o How do I rewrite a header?
         o How do I get request headers?
         o How do I delete a file?
         o How do I exit the code?
         o How do I run AJAX?
         o How do I add a mime-type?
         o What is my IP address?
         o How do I include a file?
         o How do I execute SQL?
         o How do I drop a database?
         o pow_file( readfile )
         o pow_file_put_contents( filename, contents, rwa_flags )
         o file_delete( filename )
         o pow_header( line )
         o pow_get_request_header( label )
         o pow_exit( )
         o pow_run_ajax( )
         o pow_include( filename )
         o Server variables

NAME

POW - Plain Old Webserver

SYNOPSIS

Use POW to distribute files. POW is started by clicking on the blue square in 
the bottom right-hand corner of your browser. When the play button appears, 
the server is on. Also, you can manage POW access privileges and other 
advanced features in the menu, "Tools > POW > Manage POW..."

DESCRIPTION

How do I manage POW?

You can reach the management window through 'Tools > POW > Manage POW ...'

Password Protect Site

   Check this to prevent access except through the listed username and password. 

Login ID & Password

   These will be required by anyone accessing the site if 'Password Protect Site' is checked. 

Port

   The standard port is 6670, as in localhost:6670. Do not use ports under 1025, unless you know what you are doing. 

Add Mime Type

   Add extra mime-types, like 'audio/mpeg' for the mp3 extension. Remember to check the binary checkbox, if it is a non-text format. Use the 'Add' button, not enter to add a mime-type. 

Where are my files?

Make a shortcut to this folder for easy access.

       On a Mac:
       Users/YOUR NAME/Library/Application Support/Firefox/Profiles/RANDOM 
       TEXT/default/pow/htdocs
       > ln -s /Users/YOUR NAME/Library/Application\ Support/Firefox/Profiles/
       RANDOM.default/pow/htdocs ./pow_docs
       On a PC:
       C:\Documents and Settings\YOUR NAME\Application Data\Mozilla\Firefox\
       Profiles\RANDOM TEXT\pow\htdocs
       On a Linux:
       /home/USER NAME/.mozilla/firefox/default.NUMBER/pow/htdocs
       You can check the exact path at 'http://localhost:6670/get_path.sjs'

Why can't I access my web server from the outside?

You might have a firewall blocking your server, so you can only access it from the server itself. Punch a hole in the firewall if your sys-admin allows. Try a different port in 'Tools > Pow > Manage Pow'. You might also find yourself behind NAT, in which case you are out of luck.

Can I create dynamic content?

Yes. SJS is a Javascript-based server-side scripting language. Code enclosed in ``<?sjs ?> is executed by Firefox on the server side. Try this, and save it to htdocs/test.sjs:

       <?sjs
               document.writeln("Hello world!");
       ?>

How do I read a file?

       <?sjs
               var contents = pow_file("/pow/htdocs/pow.css");
               document.writeln(contents);
       ?>
       OR
       <?sjs
               var contents = pow_file("pow.css");
               document.writeln(contents);
       ?>
       OR
       <?sjs
               var contents = pow_file('http://www.yahoo.com/');
               document.writeln(contents);
       ?>

How do I write a file?

       <?sjs
               var text = "This is a test.\n";
               // TO WRITE TO A NEW FILE
               pow_put_file_contents("file.txt", text, "w" );
               var text = "Here is another line.";
               // TO APPEND
               pow_put_file_contents("file.txt", text, "wa" );
       ?>

How do I rewrite a header?

       <?sjs
               pow_header("Content-Type: text/plain");
               document.write("OK
OK");  ?> OR to add a header <?sjs pow_header("X-Header: 123fd1294"); pow_header("Set-Cookie: session=d1294");  ?>

How do I get request headers?

       document.writeln("Headers are: "+pow_server.REQUEST);
       OR
       document.writeln("Cookie is "+get_request_header('Cookie'));

How do I delete a file?

       pow_file_delete("file.txt");

How do I exit the code?

       pow_exit();

How do I run AJAX?

Note: The AJAX API is not yet frozen.

       <?sjs
               pow_server.put_text = function() {
                       var date = new Date();
                       var time  = date.getSeconds();
                       var changetext = new Array('changeme', time);
                       return changetext;
               }
               pow_run_ajax();
       ?>
       <form onsubmit="pow_ajax_load('http://localhost:6670/test.sjs?AJAX=true'); 
               return false;" action="#" method='GET' >
               <input type='submit' value='Go' />
       </form>
Text to change
       </body>
       </html>

How do I add a mime-type?

       Additional mime-types can be added in the POW Options window at "Tools > 
       POW > Manage POW..." Use the binary checkbox for binary data, such as 
       music and images.

What is my IP address?

       See http://www.ipchicken.com/

How do I include a file?

       <?sjs
               pow_include("lib.sjs");
       ?>

How do I execute SQL?

The SJS version of Mozilla Storage cleans up after itself, casts all numbers to strings in returns from the DB, and allows access through mysql-like pretty printing or a string-only array. SQL is compatible with SQLite. If you need binary data support, use the Storage API directly.

       <?sjs
       try {
               var pdb = new pow_DB("object_db");
        pdb.exec("CREATE TABLE foo ('object_id' int, 'name' varchar(10), 
               'name2' varchar(12))");
       } catch  (e) {
        // noop
       }
       pdb.exec("INSERT INTO foo (object_id, name, name2) VALUES (1, 'gus', 'bob')");
       var results = pdb.exec("SELECT * FROM foo WHERE 1=1");
       var res = pdb.pretty_print(results);
       document.write(res);
       document.writeln(results["column_names"].join(" ")+"
"); for (var i=0;i<results.length;i++) { document.writeln(results[i].join(" ")+"
"); }
       ?>

How do I drop a database? <?sjs var pdb = new pow_DB(``object_db); pdb.drop_database(``object_db); ?>

pow_file( readfile )

readfile

           File to read. Root (/) is extensions directory.

returns

           File contents or "ERROR" if not found.

pow_file_put_contents( filename, contents, rwa_flags )

filename

           File to write.

contents

           Body to write to file.

returns

           nothing

rwa_flags

   rwa_flags can be the following:

w Write new ASCII file wb Write new binary file wba Write binary, append

file_delete( filename )

readfile

           File to delete.

returns

           nothing

pow_header( line )

Adds a response header.

line

           Header line to add. Location and Content-Type only replace 
           their default headers.

returns

           nothing

pow_get_request_header( label )

label

           Request header key. E.g., "Accept: */*"

returns

           Header value.

pow_exit( )

Stops printing to output stream. Discontinues script.

returns

           nothing

pow_run_ajax( )

       Prints needed functions to run AJAX updates to a webpage.
       See example.

returns

           nothing

pow_include( filename )

filename

           SJS file to include inline. Do not include <?sjs and ?> delimiters
           in this file.

returns

           nothing

Server variables

pow_server.REQUEST Raw request headers and POST content pow_server.GET Query string key-value pairs pow_server.RAW_POST Complete POST body if POST request is made pow_server.POST_FILENAME Filename of uploaded file pow_server.REMOTE_HOST IP address of requesting client

Personal tools
Navigation