servlets.js

1.0.25 • Public • Published

JavaScript Servlets NO LONGER SUPPORTED - Recommend using achieve server instead - it's up-to-date.

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 Session 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 (session) { 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 = session.parms.myParm;  // or
    var myParm = session.parms['myParm'];

Servlets can use other functions:


exports.servlet = function (session)  {
  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 (session) { var other = session.load("otherfile.js"); // Extends servlet features to otherfile; reloads if cache is stale. return other.hello(); }

The Servlet Session

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


  session.request    // The session request object.
  session.response   // The session response object.
  session.parms      // Parameters sent with the request
  session.dirPath    // The current application path on your computer
  session.load       // The JavaScript Servlet load() method (see above)

Dependents (0)

Package Sidebar

Install

npm i servlets.js

Weekly Downloads

14

Version

1.0.25

License

MIT

Unpacked Size

24.9 kB

Total Files

3

Last publish

Collaborators

  • rogerfgay