Electrode Hapi Web Server
Electrode Server is a configurable web server built atop Hapi.js and Node.js, designed to standardize the setup and serving of web applications. The current stable version is 3.3.0. It provides a baseline Hapi server instance that can be extended primarily through configuration, reducing the need for boilerplate code and duplicated server setup logic across projects. While offering default settings that allow for a quick startup, its strength lies in its extensive configuration options, managed optionally by tools like `electrode-confippet`, enabling fine-grained control over Hapi server and connection settings, including port, host, compression, and plugin registration. It simplifies the integration of Hapi plugins by providing a structured way to register them. Electrode Server aims to abstract away common server setup concerns, allowing developers to focus on application-specific features and logic. The package does not explicitly detail its release cadence, but minor versions are released as needed.
Common errors
-
Error: Cannot find module 'electrode-server'
cause The package `electrode-server` has not been installed.fixRun `npm install electrode-server` or `yarn add electrode-server` in your project directory. -
TypeError: require(...) is not a function
cause Attempting to call `require('electrode-server')` as a function, but the module might not be correctly exported or Node.js versions mismatch.fixEnsure you are using Node.js v8.x.x+ as required by the module. Verify `electrode-server` is installed correctly. -
Error: plugin 'your-plugin-name' missing a 'register' property
cause A Hapi plugin defined in the `plugins` configuration is missing the required `register` function, or the module path is incorrect.fixVerify that your Hapi plugin module correctly exports a `register` function and that the `module` or `register` property in your `electrode-server` plugin configuration points to the correct location/function.
Warnings
- breaking The minimum Node.js version required has been updated from `>= 4.2.2` to `v8.x.x+` (as stated in the README). Running on older Node.js environments will lead to errors.
- breaking The `connections` configuration object, which previously allowed defining multiple connections, has been deprecated. Only a single connection object (`connection`) is now supported, implying a single server instance. Migrating from older versions requires adjusting your server configuration.
- gotcha This package is implemented as a CommonJS module. Attempting to use ES Modules `import` syntax will result in `ERR_REQUIRE_ESM` or similar errors.
- gotcha When running the server with default settings, accessing `http://localhost:3000` will return a `404 Not Found` response because no routes are registered by default. You must configure Hapi plugins to add routes and handlers.
Install
-
npm install electrode-server -
yarn add electrode-server -
pnpm add electrode-server
Imports
- ElectrodeServerFunction
import startServer from 'electrode-server';
const startServer = require('electrode-server'); - DirectServerStart
import 'electrode-server';
require('electrode-server')(); - ConfiguredServerStart
import electrodeServer from 'electrode-server'; electrodeServer({ connection: { port: 9000 } });const config = { connection: { port: 9000 } }; require('electrode-server')(config);
Quickstart
const config = {
connection: {
host: process.env.HOST ?? 'localhost',
port: parseInt(process.env.PORT ?? '3000', 10),
routes: {
cors: true
}
},
plugins: {
'my-routes-plugin': {
register: (server, options) => {
server.route({
method: 'GET',
path: '/',
handler: (request, h) => {
return 'Hello, Electrode Server!';
}
});
},
options: {}
}
}
};
require('electrode-server')(config);
console.log(`Electrode Server running on http://${config.connection.host}:${config.connection.port}`);