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.
- 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.
Install Node.js v8.1 or later. (Developed / tested with v8.9.4)
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.
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
// 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.
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.
var myParm = context.parms.myParm; // or
var myParm = context.parms['myParm'];
exports.servlet = function (context) {
return hello();
}
function hello () {
return "Hello World!";
}
// 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(); }
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.