{"id":16384568,"url":"https://github.com/nashaofu/hserver","last_synced_at":"2025-06-10T08:04:42.682Z","repository":{"id":57267169,"uuid":"73811726","full_name":"nashaofu/hserver","owner":"nashaofu","description":"this is a web app framework","archived":false,"fork":false,"pushed_at":"2021-08-31T01:56:50.000Z","size":43,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-02T17:55:08.501Z","etag":null,"topics":["framework","hserver","http","http-server","nodejs","server"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nashaofu.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-11-15T12:31:20.000Z","updated_at":"2021-08-31T01:56:52.000Z","dependencies_parsed_at":"2022-08-25T02:51:48.704Z","dependency_job_id":null,"html_url":"https://github.com/nashaofu/hserver","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nashaofu%2Fhserver","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nashaofu%2Fhserver/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nashaofu%2Fhserver/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nashaofu%2Fhserver/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nashaofu","download_url":"https://codeload.github.com/nashaofu/hserver/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nashaofu%2Fhserver/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":259033827,"owners_count":22795769,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["framework","hserver","http","http-server","nodejs","server"],"created_at":"2024-10-11T04:11:46.530Z","updated_at":"2025-06-10T08:04:42.657Z","avatar_url":"https://github.com/nashaofu.png","language":"JavaScript","readme":"# hserver\nthis is a web app framework\n\n## Install\n`npm install hserver --save`\n\n## Example\n1. Hello World\n    ```javascript\n    'use strict';\n    const Server = require('hserver');\n    const port = 80;\n    var app = new Server();\n\n    app.use(function(next){\n        this.body = 'Hello World';\n        return next();\n    });\n\n    app.use(function(next) {\n        this.body += '!';\n        return next();\n    });\n\n    app.listen(port);\n    console.log('Server is running at http://127.0.0.1:' + port + '/');\n    ```\n\n2. A simple file server\n    ```javascript\n    'use strict';\n    const Server = require('hserver');\n    const path = require('path');\n    const fs = require('fs');\n    const mime = require('mime-types');\n    const port = 8080;\n\n    var app = new Server();\n\n    app.use(function (next) {\n        let pathname = decodeURI(this.request.pathname);\n        if (pathname.slice(-1) === '/') {\n        pathname = path.join(pathname, 'index.html');\n        }\n        pathname = path.join('F:\\\\Git\\\\layouter', pathname);\n        fs.stat(pathname, (err, stats) =\u003e {\n        if (err) {\n            this.response.status = 404;\n        } else {\n            if (stats.isFile()) {\n            let type = mime.lookup(pathname);\n            let charset = mime.charsets.lookup(type);\n            this.response.set('Content-Type', type + (charset ? '; charset=' + charset : ''));\n            this.body = fs.createReadStream(pathname);\n            this.response.charset = 'utf-8';\n            } else if (stats.isDirectory()) {\n            this.status = 301;\n            this.response.set('Location', path + '/');\n            } else {\n            this.status = 400;\n            }\n        }\n        next();\n        });\n    });\n    app.listen(port);\n    ```\n\n3. hserver-static middleware([hserver-static](https://github.com/nashaofu/hserver-static))\n\n    `npm install hserver-static --save`\n    ```javascript\n    'use strict';\n    const Hserver = require('hserver');\n    const Hstatic = require('hserver-static');\n\n    const port = 8081;\n    const app = new Hserver();\n\n    // static middleware\n    app.use(Hstatic({\n        // 定义访问路径前缀\n        // default ''\n        router: '/static',\n        // 定义根文件目录\n        // default '.'\n        root: 'F:\\\\Web\\\\LayoutDesigner',\n        // 定义index文件\n        // default 'index.html'\n        index: 'index.html',\n        // 允许访问method ['GET', 'POST', 'HEAD', 'DELETE', 'PUT']\n        // default ['GET', 'HEAD']\n        method: ['GET', 'HEAD'],\n        // 是否启用文件gzip压缩 Array|true|false\n        // ['deflate', 'gzip']\n        // 为true时默认为['deflate', 'gzip']\n        // 为false时，关闭gzip压缩\n        // default false\n        zip: true,\n        // 缓存时间 time(s)|true|0\n        // 为true时，默认缓存时间为7200s\n        // 为0时不缓存\n        // default 0\n        cache: 7200,\n        // etag true|false\n        // default false\n        etag: true\n    }));\n    app.listen(port);\n    console.log(`Server is running at http://127.0.0.1:${port}/`);\n    ```\n\n## API\n#### app.use()\naccept a function as argument,It is used as a middleware for loading,\nand argunment function bind context as inner this.\nthe function's argunmet next can load next middleware.\n\n* `context.app` -the server application\n* `context.request` -the packaging of the request\n* `context.response` -the packaging of the response\n* `context.req` -the node origin request\n* `context.res` -the node origin response\n* `context.url` -the request url\n* `context.method` -the request method\n* `context.accept` -the request accept\n* `context.status` -the response status\n* `context.body` -the response body\n* `context.headerSent` -the response headerSent\n* `context.writable` -the request writable\n* `context.get` -get request header\n* `context.set` -set request header\n    \n#### app.listen() \ncan start a node origin http server.\n\n#### app.callback()\na request handle function,you can use it at origin http server,such as\n    \n```javascript\nconst http = require('http');\nconst server = http.createServer(this.callback());\nserver.listen(8080);\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnashaofu%2Fhserver","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnashaofu%2Fhserver","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnashaofu%2Fhserver/lists"}