This page and the plugin itself are still in development, a lot will change.
SoapExpress is a utility package designed to streamline the integration of SoapJS with Express, providing a structured approach to creating and managing Express servers, routes, and authentication. With built-in support for dependency injection and a suite of authentication validators, SoapExpress simplifies the process of building robust, secure APIs.
Features
Seamless integration with SoapJS and Express.
Custom SoapExpressRouter for easy route management.
SoapExpressServer for initializing and running Express applications.
SoapAuthManager for straightforward authentication using various strategies.
Supports dependency injection containers.
Simplified authentication setup with pre-defined validators.
Getting Started
To get started with SoapExpress, follow these steps to set up and run your server.
Installation
First, install the package along with its peer dependencies if you haven't already:
npm install @soapjs/soap-express @soapjs/soap express
npm install inversify reflect-metadata
# OR use soap-cli to build new project
soap new project
Setup
Configure Your Server and Router
Create a new file (e.g., index.ts) and import the necessary classes and dependencies. Define your server configuration, create a container for dependency injection, and initialize your custom router and dependencies.
// index.tsimport'reflect-metadata';import*as Soap from'@soapjs/soap';import { SoapExpressServer } from'@soapjs/soap-express';import express from'express';import { Container } from'inversify';// Import or define your custom router and dependenciesimport { Router } from'./router';import { Dependencies } from'./dependencies';constconfig=Config.create();constserver=newSoapExpressServer(config);// Create ioc containerconstcontainer=newContainer();// Create dependenciesconstdependencies=newDependencies(container);dependencies.configure();// Create routerconstrouter=newRouter(server.app, container, config);router.configure();// Run serverserver.start();
Extend SoapExpressRouter
Extend SoapExpressRouter to define your routes. Use the provided SoapAuthManager for authentication and validators for different strategies (JWT, Basic Auth, OAuth, etc.).
// router.tsimport { SoapExpressRouter, AuthValidators } from'soap-express';import { Container } from'inversify';exportclassRouterextendsSoapExpressRouter<Container> {constructor(framework:Express, container:Container, config:Config) {// Auth validators are not mandatory;// if your API does not require authentication, omit the last argument.super( framework, container, config, { basic:newYourBasicValidator(container) } ); }configure() {// In this method you need to bind all the routes as follows:constusersController=this.container.get<UsersController>(UsersController.Token);this.mount(UsersRoute.create(usersController.getUser.bind(usersController),this.config), );// IF you use soap CLI to create routes or controller handlers, the binding is done for you }}
Define Dependencies
Define a class to manage your application's dependencies.
// dependencies.tsimport { Container } from'inversify';import*as Soap from'@soapjs/soap';exportclassDependenciesimplementsSoap.Dependencies {constructor(protected container:Container,protected config:Config){}configure() {// In this method you need to bind all the dependencies as follows:container.bind<UserController>(UserController.Token).to(UserController);// IF you use soap CLI to create domain components, the binding is done for you }}
Running the Server
Run your server using Node.js or a process manager like PM2. Ensure that your entry point (e.g., index.js) is correctly specified.
nodeindex.js
Your Express server should start, and it will log the listening port to the console.