Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@api.global/typedserver

Package Overview
Dependencies
Maintainers
0
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@api.global/typedserver

A TypeScript-based project for easy serving of static files with support for live reloading, compression, and typed requests.

  • 3.0.51
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
0
Created
Source
# @api.global/typedserver
Easy serving of static files

## Install
To install @api.global/typedserver, run the following command in your terminal:

```bash
npm install @api.global/typedserver --save

This will add @api.global/typedserver to your project's dependencies.

Usage

@api.global/typedserver is designed to make serving static files and handling web requests in a TypeScript environment easy and efficient. It leverages Express under the hood, providing a powerful API for web server creation with additional utilities for live reloading, typed requests/responses, and more, embracing TypeScript's static typing advantages.

Setting up a Basic Web Server

The following example demonstrates how to set up a basic web server serving files from a directory.

import { TypedServer } from '@api.global/typedserver';

const serverOptions = {
  port: 8080, // Port to listen on
  serveDir: 'public', // Directory to serve static files from
  watch: true, // Enable live reloading of changes
  injectReload: true, // Inject live reload script into served HTML files
  cors: true // Enable CORS
};

const typedServer = new TypedServer(serverOptions);

async function startServer() {
  await typedServer.start();
  console.log(`Server is running on http://localhost:${serverOptions.port}`);
}

startServer().catch(console.error);

In the example above, TypedServer is instantiated with an IServerOptions object specifying options like the port to listen on (8080), the directory containing static files to serve (public), and live reload features. Calling start() on the typedServer instance initiates the server.

Using Typed Requests and Responses

TypedServer supports typed requests and responses, making API development more robust and maintainable. Define your request and response types, and use them to type-check incoming requests and their responses.

First, define the types:

// Define a request type
interface MyCustomRequest {
  userId: string;
}

// Define a response type
interface MyCustomResponse {
  userName: string;
}

Next, set up a route to handle requests using these types:

import { TypedRouter, TypedHandler } from '@api.global/typedrequest';

// Instantiate a TypedRouter
const typedRouter = new TypedRouter();

// Register a route with request and response types
typedRouter.addTypedHandler<MyCustomRequest, MyCustomResponse>(
  new TypedHandler('getUser', async (requestData) => {
    // Implement your logic here. For example, fetch user data from a database.
    const userData = { userName: 'John Doe' }; // Dummy implementation
    return { response: userData };
  })
);

// Bind the typed router to the server
typedServer.useTypedRouter(typedRouter);

// Now, the route is set up to handle requests with type checking

This example shows defining types for requests and responses, creating a TypedRouter, and adding a route with typed handling. This feature brings the benefits of TypeScript's static type checking to server-side logic, improving the development experience.

Enabling SSL/TLS

To enable SSL/TLS, configure the TypedServer with the SSL options, including the paths to your SSL certificate and key files:

const serverOptions = {
  port: 443,
  serveDir: 'public',
  watch: true,
  injectReload: true,
  cors: true,
  privateKey: fs.readFileSync('path/to/ssl/private.key'),
  publicKey: fs.readFileSync('path/to/ssl/certificate.crt')
};

const typedServer = new TypedServer(serverOptions);
startServer().catch(console.error);

Replace 'path/to/ssl/private.key' and 'path/to/ssl/certificate.crt' with the actual paths to your SSL key and certificate files. This setup ensures that your server communicates over HTTPS, encrypting the data transmitted between the client and the server.

Conclusion

@api.global/typedserver offers a streamlined way to set up a web server with TypeScript, featuring static file serving, live reloading, typed request/response handling, and SSL support. This guide covers the basic usage, but TypedServer is highly configurable, catering to various hosting and development needs.


For a deeper dive into the API and more advanced configurations, refer to the official documentation and type definitions included in the package.

## License and Legal Information

This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license](license) file within this repository. 

**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.

### Trademarks

This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.

### Company Information

Task Venture Capital GmbH  
Registered at District court Bremen HRB 35230 HB, Germany

For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.

By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.

Keywords

FAQs

Package last updated on 27 Aug 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc