Starting the server
achieve supports a variety of options that only work if they are configured or turned on. Only one is mandatory: setAppPath(appPath). It defines the base directory for public resources—the root of your application space.
Achieve restricts HTTP request path resolution to resources within the application space. Backend programs can still access resources elsewhere on the computer when appropriate. appPath can be any accessible directory.
In the following examples, with default routing, request URL /hello/hello.htm maps to c:/my/app/path/hello/hello.htm.
Optional configuration settings appear after setAppPath(). Details on configuration options are given in the configuration reference.
HTTP
In CommonJS:
const achieve = require("achieve");
achieve.setAppPath("c:/my/app/path/");
// optional settings
achieve.listen(port); // port defaults to 80
In ESM:
import achieve from "achieve";
achieve.setAppPath("c:/my/app/path/");
// optional settings
achieve.listen(port); // port defaults to 80
Save the startup file as either start.js (for example) for CommonJS or start.mjs for ESM.
From that directory, run node start for CommonJS or node start.mjs for ESM.
You should see the startup message from achieve. The server is then running.
HTTPS
const options = {
key: /* private key */,
cert: /* certificate */,
httpsPort: 8443 // default 443
};
achieve.slisten(options);
HTTP/2 with TLS
const options = {
key: /* private key */,
cert: /* certificate */,
http2Port: 8444 // default 443
};
achieve.listen2(options);
HTTP/2 without TLS
achieve.listen2(port); // port defaults to 80
HTTPS and HTTP/2 with TLS require a private key and certificate. For HTTPS, httpsPort specifies the port. For HTTP/2 with TLS, http2Port specifies the port. In both cases, the port defaults to 443 when omitted.
Multiple listeners
You can start more than one listener in the same startup file, so long as each uses its own available port. The listeners share the same achieve configuration and application space.
For multiple processes on one port, see Clustering in the Advanced Demonstrations section.