npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@api.global/typedserver

v3.0.29

Published

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

Downloads

460

Readme

# @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 [email protected].

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.