0.3.8 • Published 1 year ago

@fnet/node-express v0.3.8

Weekly downloads
-
License
MIT
Repository
gitlab
Last release
1 year ago

@fnet/node-express

This project provides a basic, customizable framework for setting up a Node.js and Express.js server. It focuses on integrating essential middleware for creating secure and efficient web applications. Users can benefit from a pre-configured setup that manages common web app functionalities such as Cross-Origin Resource Sharing (CORS), session management, security headers, and JSON parsing.

How It Works

The project sets up a Node.js server using Express. It integrates several middleware components to handle standard tasks for web applications. The setup includes features like request parsing, IP address detection, CORS support, and session handling with Redis or memory-based fallback. It also incorporates security practices through various helmet configurations to safeguard the application in production environments.

Key Features

  • CORS Support: Configurable CORS middleware to control resource sharing across different origins.
  • Session Management: Supports session persistence using Redis with a fallback to in-memory storage if Redis is unavailable.
  • Security Headers: Utilizes Helmet middleware to apply security headers, enhancing basic security measures.
  • JSON Body Parsing: Handles JSON request bodies with a defined size limit.
  • IP Address Detection: Provides middleware to detect request IP addresses.
  • Health Check Endpoint: Implements a simple /healthz route to verify server health.

Conclusion

This project is particularly useful for developers who need a straightforward and secure Express.js setup with essential middleware for web applications. It lays the groundwork for handling common server-side tasks, making it easier and faster to build a new web server while adhering to good security practices.

Developer Guide for @fnet/node-express

Overview

The @fnet/node-express library provides a streamlined method for setting up a Node.js server using Express. It simplifies the configuration of common middleware, such as CORS, security headers, and session management, making it easier for developers to quickly build and deploy robust HTTP servers. Key features include:

  • Express server setup with JSON parsing and query parsing.
  • Configurable CORS handling.
  • Session management using Redis or an in-memory store as a fallback.
  • Security enhancements through Helmet for setting HTTP headers.
  • IP request logging.
  • A basic /healthz endpoint for health checks.

Installation

To install the library, use either npm or yarn:

npm install @fnet/node-express

or

yarn add @fnet/node-express

Usage

Here's a basic example of how to use the @fnet/node-express library to create a server with custom middleware:

import expressLib from '@fnet/node-express';

(async () => {
  const args = {
    server_port: 3000,
    cors_origin_whitelist: 'http://example.com',
    session_secret: 'my-secret-key',
    isProduction: false, // Boolean indicating if running in production
    apis: [
      {
        use({ app }) {
          // Define custom middleware or routes here
          app.get('/hello', (req, res) => {
            res.send('Hello World');
          });
        }
      }
    ]
  };

  const serverContext = await expressLib(args);

  serverContext.start(); // Starts the server
})();

Examples

Setting Up a Basic Server

import expressLib from '@fnet/node-express';

const runServer = async () => {
  const context = await expressLib({
    server_port: 4000, 
    apis: [
      {
        use({ app }) {
          app.get('/api/status', (req, res) => {
            res.json({ status: "Server is running." });
          });
        }
      }
    ]
  });
  context.start();
};

runServer();

Using Session Management

import expressLib from '@fnet/node-express';

const runWithSession = async () => {
  const context = await expressLib({
    session_secret: 'supersecret!', // Your session secret
    redis_host: 'localhost', // Redis host
    redis_port: 6379, // Redis port
    session_name: 'my-session' // Session name
  });
  context.start();
};

runWithSession();

Customizing CORS

import expressLib from '@fnet/node-express';

const customizeCors = async () => {
  const context = await expressLib({
    cors_origin_whitelist: 'https://myapp.com',
    cors_credentials: true
  });
  context.start();
};

customizeCors();

Acknowledgement

The @fnet/node-express library leverages several excellent open-source libraries, such as Express for handling HTTP requests, Helmet for security, and CORS for middleware. These libraries provide the backbone for this library, making it possible to simplify server creation and configuration.

Input Schema

$schema: https://json-schema.org/draft/2020-12/schema
type: object
properties:
  server_port:
    type: integer
    description: Port on which the server will listen
    default: 8080
  cors_origin_whitelist:
    type: string
    description: List of whitelisted origins for CORS, separated by commas
  cors_credentials:
    type: boolean
    description: Whether CORS requests can include credentials
  cors_max_age:
    type: integer
    description: Maximum age (in seconds) for CORS preflight requests
    default: 3600
  cors_allowed_headers:
    type: string
    description: Headers that are allowed in CORS requests
  cors_methods:
    type: string
    description: HTTP methods that are allowed in CORS requests
  cors_exposed_headers:
    type: string
    description: Headers that are exposed in CORS responses
  redis_host:
    type: string
    description: Hostname for the Redis server
  redis_port:
    type: integer
    description: Port for the Redis server
    default: 6379
  redis_store_prefix:
    type: string
    description: Prefix for Redis store keys
    default: "rsp:"
  session_secret:
    type: string
    description: Secret key for session management
  session_name:
    type: string
    description: Custom session name
  session_cookie_domain:
    type: string
    description: Domain for session cookies
  isProduction:
    type: boolean
    description: Whether the environment is production
  apis:
    type: array
    description: List of APIs to initialize
    items:
      type: object
      properties:
        use:
          type: string
          description: Function name used to initialize API
        onReady:
          type: string
          description: Function name called when API is ready
  mode:
    type: string
    description: Mode operation for the server
    default: start
0.3.6

1 year ago

0.3.5

1 year ago

0.3.8

1 year ago

0.3.7

1 year ago

0.3.2

1 year ago

0.3.1

1 year ago

0.3.4

1 year ago

0.3.3

1 year ago

0.2.13

1 year ago

0.2.12

1 year ago

0.1.20

1 year ago

0.2.11

1 year ago

0.2.10

1 year ago

0.2.1

1 year ago

0.2.7

1 year ago

0.2.6

1 year ago

0.2.9

1 year ago

0.2.8

1 year ago

0.2.3

1 year ago

0.2.2

1 year ago

0.2.5

1 year ago

0.2.4

1 year ago

0.1.18

2 years ago

0.1.19

2 years ago

0.1.17

2 years ago

0.1.16

2 years ago

0.1.11

2 years ago

0.1.12

2 years ago

0.1.13

2 years ago

0.1.14

2 years ago

0.1.15

2 years ago

0.1.10

2 years ago

0.1.9

2 years ago

0.1.8

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.5

2 years ago

0.1.4

2 years ago

0.1.3

2 years ago

0.1.2

2 years ago

0.1.1

2 years ago