typedserver/readme.md

134 lines
5.6 KiB
Markdown
Raw Permalink Normal View History

2024-04-14 17:00:39 +00:00
```markdown
# @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.
2023-03-29 12:54:07 +00:00
## Usage
2024-04-14 17:00:39 +00:00
`@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.
```typescript
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:
```typescript
// 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:
```typescript
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);
2023-03-29 12:54:07 +00:00
2024-04-14 17:00:39 +00:00
// 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
2023-03-29 12:54:07 +00:00
2024-04-14 17:00:39 +00:00
To enable SSL/TLS, configure the `TypedServer` with the SSL options, including the paths to your SSL certificate and key files:
2023-03-29 12:54:07 +00:00
2024-04-14 17:00:39 +00:00
```typescript
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);
2023-03-29 12:54:07 +00:00
```
2024-04-14 17:00:39 +00:00
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
2023-03-29 12:54:07 +00:00
2024-04-14 17:00:39 +00:00
Task Venture Capital GmbH
Registered at District court Bremen HRB 35230 HB, Germany
2023-03-29 12:54:07 +00:00
2024-04-14 17:00:39 +00:00
For any legal inquiries or if you require further information, please contact us via email at hello@task.vc.
2023-03-29 12:54:07 +00:00
2024-04-14 17:00:39 +00:00
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.