js-expressify
v1.0.3
Published
A lightweight and efficient Node.js module for simplified Express.js server creation and management.
Downloads
1
Maintainers
Readme
Documentation
The js-expressify
module is a tool that simplifies the creation and management of Express.js-based web servers in a Node.js application. Below are the main features and how to use them:
Installation
Make sure Node.js is installed on your system before using this module. You can install it using npm or yarn.
npm install js-expressify
Importing the Module
First, import the module into your Node.js file to start using it:
const ServerBuilder = require("js-expressify");
const serverBuilder = new ServerBuilder();
Server Creation:
This module allows you to create Express.js servers easily. You can define the server name, your controller paths, and the port you want the server to listen on. If the specified port is busy, the module will automatically make additional attempts by adding one to the port number with each attempt until an available port is found. A maximum of 10 attempts are made to create the server.
async function createServer() {
const ServerBuilder = require("js-expressify");
const serverBuilder = new ServerBuilder();
const serverConfig = {
name: "myServer",
routers: "./routes", // Path to the file with your application paths
port: 3000, // Port on which the server is running
pathViews: "./my/view/path",
pathPublic: "./my/public/path",
};
try {
const result = await serverBuilder.newServer(serverConfig);
if (result) {
console.log(
`Server ${serverConfig.name} is running on port ${serverConfig.port}`
);
} else {
console.error(`Could not start server ${serverConfig.name}`);
}
} catch (error) {
console.error(
`Error creating server ${serverConfig.name}: ${error.message}`
);
}
// You can add more servers with different names and configurations.
}
createServer();
Defining Routes in the routes.js
File
The routes.js
file is crucial for configuring routes for your server in "js-expressify". Follow these guidelines to define effective routes:
Create an array named
routes
to store the server's routes. Each route is defined as an object within this array.Each route object must have three main properties:
method
: Specifies the HTTP method used to access this route (e.g., "get," "post," "put," "delete," etc.).path
: Indicates the URL or relative path that triggers this route when accessed from the browser.handler
: Contains a function that will execute when the corresponding route is accessed. This function takes two arguments:req
(the client's request) andres
(the response to be sent to the client).
For example, the following code defines a "get" route that responds to the root path ("/") and sends a welcome message to the client when that route is accessed:
let routes = [ { method: "get", path: "/", handler: (req, res) => { res.send("Welcome to the myApp Home page!"); }, }, { method: "get", path: "/about", handler: (req, res) => { res.send("Welcome to the About page"); }, }, // You can add more routes this way ]; module.exports = routes;
Stopping a Server
You can stop a running server using the stopServer
method. When you stop a server, it is deleted and if you decide to create it again, the module will automatically reload the routes code without needing to restart the application.
Example of stopping a server:
async function stopServer(serverNameToStop) {
try {
const result = await serverBuilder.stopServer(serverNameToStop);
if (result) {
console.log(`Server ${serverNameToStop} stopped successfully.`);
} else {
console.error(`Could not stop server ${serverNameToStop}.`);
}
} catch (error) {
console.error(
`Error stopping server ${serverNameToStop}: ${error.message}`
);
}
}
const serverNameToStop = "myServer"; // Nombre del servidor que deseas detener
stopServer(serverNameToStop);
Additional Notes
js-expressify
greatly simplifies the configuration and management of web servers, allowing you to focus on developing your application instead of worrying about the complexity of configuring the Express.js server.
You can customize the view and public static file paths by providing
pathViews
andpathPublic
when creating a server.The
isPortAvailable
function is used to check if a specific port is available before starting a server on that port.The module also includes additional functionalities that are commented and labeled as
coming soon
in the source code, suggesting they may be available in future versions.This module is especially useful when combined with Electron.js to develop desktop applications. When used in conjunction with Electron.js, you can create desktop applications that also function as local web servers, opening the door to a wide range of possibilities in developing interactive and user-centric applications.
Developer Name
- Name: devlwte
- Email: [email protected]
- GitHub profile: https://github.com/devlwte
If you have any questions or need technical support, feel free to contact the developer.
If you run into any problems or need help with the js-expressify
module, here are some options:
Report a Problem
If you think you have found a bug or a problem with the module, please create an "issue" in the official repository at GitHub. Be sure to provide the following information when reporting a problem:
- Detailed description of the problem.
- Step by step to reproduce the problem.
- Screenshots (if applicable).
- Module version and version of Node.js that you are using.
Community Support
If you have general questions about using the module or need guidance, you can post your questions in the "Discussions" section of the GitHub repository. The user community and the developer can help you with your queries.
Contact Developer
If you have more specific questions or need urgent help, you can contact the developer directly through his email: [email protected].
Please be as clear and detailed as possible when describing any issues or questions you may have. We are here to help you solve any difficulties you encounter when using js-expressify
.