update documentation
This commit is contained in:
parent
64d4fb011d
commit
5e4878e492
@ -5,12 +5,31 @@
|
|||||||
"gitzone": {
|
"gitzone": {
|
||||||
"projectType": "npm",
|
"projectType": "npm",
|
||||||
"module": {
|
"module": {
|
||||||
"githost": "gitlab.com",
|
"githost": "code.foss.global",
|
||||||
"gitscope": "pushrocks",
|
"gitscope": "pushrocks",
|
||||||
"gitrepo": "typedserver",
|
"gitrepo": "typedserver",
|
||||||
"description": "easy serving of static files",
|
"description": "A TypeScript-based project for easy serving of static files with support for live reloading, compression, and typed requests.",
|
||||||
"npmPackagename": "@api.global/typedserver",
|
"npmPackagename": "@api.global/typedserver",
|
||||||
"license": "MIT"
|
"license": "MIT",
|
||||||
}
|
"keywords": [
|
||||||
|
"TypeScript",
|
||||||
|
"static file serving",
|
||||||
|
"live reload",
|
||||||
|
"compression",
|
||||||
|
"typed requests",
|
||||||
|
"HTTP server",
|
||||||
|
"SSL",
|
||||||
|
"cors",
|
||||||
|
"express middleware",
|
||||||
|
"proxy",
|
||||||
|
"sitemap",
|
||||||
|
"feeds",
|
||||||
|
"robots.txt",
|
||||||
|
"compression (gzip, deflate, brotli)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"tsdoc": {
|
||||||
|
"legal": "\n## License and Legal Information\n\nThis 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. \n\n**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.\n\n### Trademarks\n\nThis 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.\n\n### Company Information\n\nTask Venture Capital GmbH \nRegistered at District court Bremen HRB 35230 HB, Germany\n\nFor any legal inquiries or if you require further information, please contact us via email at hello@task.vc.\n\nBy 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.\n"
|
||||||
}
|
}
|
||||||
}
|
}
|
18
package.json
18
package.json
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "@api.global/typedserver",
|
"name": "@api.global/typedserver",
|
||||||
"version": "3.0.27",
|
"version": "3.0.27",
|
||||||
"description": "easy serving of static files",
|
"description": "A TypeScript-based project for easy serving of static files with support for live reloading, compression, and typed requests.",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
"typings": "dist_ts/index.d.ts",
|
"typings": "dist_ts/index.d.ts",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
@ -15,8 +15,20 @@
|
|||||||
"url": "https://github.com/pushrocks/easyserve.git"
|
"url": "https://github.com/pushrocks/easyserve.git"
|
||||||
},
|
},
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"serve",
|
"TypeScript",
|
||||||
"browser-sync"
|
"static file serving",
|
||||||
|
"live reload",
|
||||||
|
"compression",
|
||||||
|
"typed requests",
|
||||||
|
"HTTP server",
|
||||||
|
"SSL",
|
||||||
|
"cors",
|
||||||
|
"express middleware",
|
||||||
|
"proxy",
|
||||||
|
"sitemap",
|
||||||
|
"feeds",
|
||||||
|
"robots.txt",
|
||||||
|
"compression (gzip, deflate, brotli)"
|
||||||
],
|
],
|
||||||
"author": "Lossless GmbH <office@lossless.com> (https://lossless.com)",
|
"author": "Lossless GmbH <office@lossless.com> (https://lossless.com)",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
1
readme.hints.md
Normal file
1
readme.hints.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
|
154
readme.md
154
readme.md
@ -1,49 +1,133 @@
|
|||||||
# @pushrocks/typedserver
|
```markdown
|
||||||
easy serving of static files
|
# @api.global/typedserver
|
||||||
|
Easy serving of static files
|
||||||
|
|
||||||
## Availabililty and Links
|
## Install
|
||||||
* [npmjs.org (npm package)](https://www.npmjs.com/package/@apiglobal/typedserver)
|
To install @api.global/typedserver, run the following command in your terminal:
|
||||||
* [gitlab.com (source)](https://gitlab.com/pushrocks/typedserver)
|
|
||||||
* [github.com (source mirror)](https://github.com/pushrocks/typedserver)
|
|
||||||
* [docs (typedoc)](https://pushrocks.gitlab.io/typedserver/)
|
|
||||||
|
|
||||||
## Status for master
|
```bash
|
||||||
|
npm install @api.global/typedserver --save
|
||||||
|
```
|
||||||
|
|
||||||
Status Category | Status Badge
|
This will add `@api.global/typedserver` to your project's dependencies.
|
||||||
-- | --
|
|
||||||
GitLab Pipelines | [![pipeline status](https://gitlab.com/pushrocks/typedserver/badges/master/pipeline.svg)](https://lossless.cloud)
|
|
||||||
GitLab Pipline Test Coverage | [![coverage report](https://gitlab.com/pushrocks/typedserver/badges/master/coverage.svg)](https://lossless.cloud)
|
|
||||||
npm | [![npm downloads per month](https://badgen.net/npm/dy/@apiglobal/typedserver)](https://lossless.cloud)
|
|
||||||
Snyk | [![Known Vulnerabilities](https://badgen.net/snyk/pushrocks/typedserver)](https://lossless.cloud)
|
|
||||||
TypeScript Support | [![TypeScript](https://badgen.net/badge/TypeScript/>=%203.x/blue?icon=typescript)](https://lossless.cloud)
|
|
||||||
node Support | [![node](https://img.shields.io/badge/node->=%2010.x.x-blue.svg)](https://nodejs.org/dist/latest-v10.x/docs/api/)
|
|
||||||
Code Style | [![Code Style](https://badgen.net/badge/style/prettier/purple)](https://lossless.cloud)
|
|
||||||
PackagePhobia (total standalone install weight) | [![PackagePhobia](https://badgen.net/packagephobia/install/@apiglobal/typedserver)](https://lossless.cloud)
|
|
||||||
PackagePhobia (package size on registry) | [![PackagePhobia](https://badgen.net/packagephobia/publish/@apiglobal/typedserver)](https://lossless.cloud)
|
|
||||||
BundlePhobia (total size when bundled) | [![BundlePhobia](https://badgen.net/bundlephobia/minzip/@apiglobal/typedserver)](https://lossless.cloud)
|
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
Use TypeScript for best in class instellisense.
|
`@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.
|
||||||
|
|
||||||
```javascript
|
### Setting up a Basic Web Server
|
||||||
import { TypedServer } from '@apiglobal/typedserver';
|
|
||||||
|
|
||||||
let myTypedserver = new TypedServer('/some/path/to/webroot', 8080);
|
The following example demonstrates how to set up a basic web server serving files from a directory.
|
||||||
myTypedserver.start().then(() => {
|
|
||||||
// this is executed when server is running guaranteed
|
|
||||||
myTypedserver.stop(); // .stop() will work even if not waiting for server to be fully started
|
|
||||||
});
|
|
||||||
|
|
||||||
myTypedserver.reload(); // reloads all connected browsers of this instance
|
```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);
|
||||||
```
|
```
|
||||||
|
|
||||||
## Contribution
|
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.
|
||||||
|
|
||||||
We are always happy for code contributions. If you are not the code contributing type that is ok. Still, maintaining Open Source repositories takes considerable time and thought. If you like the quality of what we do and our modules are useful to you we would appreciate a little monthly contribution: You can [contribute one time](https://lossless.link/contribute-onetime) or [contribute monthly](https://lossless.link/contribute). :)
|
### Using Typed Requests and Responses
|
||||||
|
|
||||||
For further information read the linked docs at the top of this readme.
|
`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.
|
||||||
|
|
||||||
## Legal
|
First, define the types:
|
||||||
> MIT licensed | **©** [Task Venture Capital GmbH](https://task.vc)
|
|
||||||
| By using this npm module you agree to our [privacy policy](https://lossless.gmbH/privacy)
|
```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);
|
||||||
|
|
||||||
|
// 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:
|
||||||
|
|
||||||
|
```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);
|
||||||
|
```
|
||||||
|
|
||||||
|
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.
|
||||||
|
Loading…
Reference in New Issue
Block a user