Skip to content

JavaScript Servlets provide a fast and convenient way to create back end processes on Node.js. They were developed as an integral part of Achieve; a complete server for Node.js.

License

highlevellogic/servlets.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

31 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

servlets.js

JavaScript Servlets Depricated - Totally Unsupported - Use Achieve directly instead - maybe later if Achieve is rewritten

JavaScript Servlets provide a fast and convenient way to create back end processes on Node.js. They were developed as an integral part of Achieve; a complete server for Node.js.

The Achieve tutorial provides details about using servlets, click here

Achieve and servlets.js are part of the High Level Logic Project.

Features Summary

  • Runs back-end JavaScript programs (via JavaScript Servlets).
  • Supports default index.js
  • No knowledge of Node.js required to start using JS Servlets.
  • Little knowledge of JavaScript required to start using JS Servlets.
  • JS Servlets handle HTTP Response. App just uses return statement.
  • Useful app error reports - without crashing the server.
  • Automatic reload of modified files.
  • Servlet Context Object allows developer to take complete control.
  • Node.js environment configuration. (development,production)
  • Configurable apps folder path and path to the ROOT application.

Quick Start

Install Node.js v8.1 or later. (Developed / tested with v8.9.4)

Using:


const server = require('servlets');
server.listen();  // defaults to port 80

By default, the base application directory is the same as the file you create to start the server. You can set the base application directory with the setAppPath option below.

Running servlets with options:


const server = require('servlets');

server.setAppPath("c:/myachieve/myapps"); // set base directory for all applications server.setRootDir('root'); // set a subdirectory under the root directory for THE ROOT application server.showMimeTypes(); // Show the current list of supported Mime Types server.addMimeType("pub","application/x-mspublisher"); // add an unsupported MIME type server.setNodeEnv("development"); // set Node environment

server.listen(8989); // listens on port 8989

Hello World Servlet:


// Save this code in file index.js in the apps directory ("application base" - directory where you are running the server)
exports.servlet = function (context) { return "Hello World!"; // Achieve handles the response. }

Display results in browser: http://localhost:8989 (assuming port 8989 and the file is named index.js.

Achieve will reload your programs when they've been modified. No need to restart the server.

Application Code Error Messages in browser console:

Modify your servlet to cause an error by deleting a few characters from the end of the return statement: return "Hello World. Refresh the page.

Access parameter values that were sent with the request:


    var myParm = context.parms.myParm;  // or
    var myParm = context.parms['myParm'];

Servlets can use other functions:


exports.servlet = function (context)  {
  return hello();
}
function hello ()  {
   return "Hello World!";
}

Servlets can use functions in other files.


// in otherfile.js
exports.hello () {
  Return "Hello World!";
}

// in myservlet.js exports.servlet = function (context) { var other = context.load("otherfile.js"); // Extends servlet features to otherfile; reloads if cache is stale. return other.hello(); }

The Servlet Context

You can use the Servlet Context to take control of your back end process. The Servlet Context contains:


  context.request    // The session request object.
  context.response   // The session response object.
  context.parms      // Parameters sent with the request
  context.dirPath    // The current application path on your computer
  context.load       // The JavaScript Servlet load() method (see above)
  context.allowAsync // Set to true if you handle the response in an asynchronous process.

About

JavaScript Servlets provide a fast and convenient way to create back end processes on Node.js. They were developed as an integral part of Achieve; a complete server for Node.js.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published