{"id":19326740,"url":"https://github.com/sroehrl/node-express-typescript","last_synced_at":"2025-04-22T20:32:54.749Z","repository":{"id":48541081,"uuid":"322987186","full_name":"sroehrl/node-express-typescript","owner":"sroehrl","description":"Micro framework to be used as a boilerplate for rapid API development. ","archived":false,"fork":false,"pushed_at":"2021-07-20T23:33:19.000Z","size":107,"stargazers_count":15,"open_issues_count":1,"forks_count":6,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-02T04:03:02.135Z","etag":null,"topics":["api-rest","continuous-integration","express","jest","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","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/sroehrl.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":"2020-12-20T03:42:46.000Z","updated_at":"2023-08-27T19:08:33.000Z","dependencies_parsed_at":"2022-09-04T03:41:19.048Z","dependency_job_id":null,"html_url":"https://github.com/sroehrl/node-express-typescript","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sroehrl%2Fnode-express-typescript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sroehrl%2Fnode-express-typescript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sroehrl%2Fnode-express-typescript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sroehrl%2Fnode-express-typescript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sroehrl","download_url":"https://codeload.github.com/sroehrl/node-express-typescript/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250318986,"owners_count":21411028,"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":["api-rest","continuous-integration","express","jest","typescript"],"created_at":"2024-11-10T02:14:42.693Z","updated_at":"2025-04-22T20:32:54.347Z","avatar_url":"https://github.com/sroehrl.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Express api server w/ typescript\n\n[![Maintainability](https://api.codeclimate.com/v1/badges/df25bcbd6e685e3c29bb/maintainability)](https://codeclimate.com/github/sroehrl/node-express-typescript/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/df25bcbd6e685e3c29bb/test_coverage)](https://codeclimate.com/github/sroehrl/node-express-typescript/test_coverage)\n[![Build Status](https://travis-ci.com/sroehrl/node-express-typescript.svg?branch=master)](https://travis-ci.com/sroehrl/node-express-typescript)\n\nA clean setup/boilerplate for your REST Api. This repo is aimed at providing developers with\na testable, easy to use structure for rapid API development.\n\n- typescript\n- express\n- MySQL \u0026 orm \u0026 migration\n- route loading\n- jest\n- JWT authentication\n- yarn\n\n## Content\n\n- [Installation \u0026 Setup](#installation--setup) \n- [Authentication](#authentication) \n- [Routing \u0026 Resolving](#routing-and-resolving) \n\n## Installation \u0026 Setup\n\n_Note:_ typescript und yarn are ideally installed globally\n\n- After cloning, run `yarn install`\n- Create a MySQL database for your project\n- Copy `.env_example` to `.env` and adjust its variables accordingly.\n- **optional**: adjust _models/user/migration.js_ and _models/user/UerInterface.ts_ to your needs\n- run `node migrate` to write table(s) to your database\n- run `yarn start`\n\n## Authentication\n\nThis package includes authentication middleware for JWT authentication. Authenticated endpoints \nexpect an authorization header.\n\n_Register_\n\n```javascript\n// example\nfetch('http://localhost:3000/api/auth', {\n    method: 'POST',\n    body: JSON.stringify({\n        password: '123456',\n        userName: 'demo993'\n    }),\n    headers: {\n        \"Content-type\": \"application/json; charset=UTF-8\"\n    }\n})\n```\n\n_Login_\n\n```javascript\n// example\nfetch('http://localhost:3000/api/login', {\n    method: 'POST',\n    body: JSON.stringify({\n        password: '123456',\n        userName: 'demo993'\n    }),\n    headers: {\n        \"Content-type\": \"application/json; charset=UTF-8\"\n    }\n})\n    .then(j =\u003e j.json())\n    .then(result =\u003e {\n        // retrieve JWT token\n        const JWT = result.token;\n    })\n```\n\n_Making authenticated calls_\n\n```javascript\n// example\nfetch('http://localhost:3000/api/protected', {\n    method: 'GET',\n    headers: {\n        \"Content-type\": \"application/json; charset=UTF-8\",\n        \"Authorization\": `bearer ${yourToken}`\n    }\n})\n    .then(j =\u003e j.json())\n    .then(result =\u003e {\n        console.log(result)\n    })\n```\n## Models\n\nModels expect a migration.js in order to sync database tables.\nLet's have a look at the user model as an example\n\n```javascript\nconst base = {\n    user:{ // table name\n        id: 'binary(16) NOT NULL',\n        userName: 'varchar(40) DEFAULT NULL',\n        password: 'varchar(255) DEFAULT NULL',\n        createdAt: 'timestamp NULL DEFAULT current_timestamp()',\n        deletedAt: 'datetime DEFAULT NULL'\n    },\n    user_role:{} // another table\n}\n\nmodule.exports = {\n    base,\n    migrations: [] // alterations to existing models should be placed in migrations array\n}\n```\nSimple models (one db table) do not need a model class and can simply be created at runtime\nusing /utils/resolver.ts _(used in all auth routes)_\n\n## Routing and Resolving\n\nFiles in /src/routes are loaded automatically. \n\n```javascript\n// e.g. /src/routes/comments.ts\nimport {Express, Request, Response} from 'express-serve-static-core';\nimport Resolver from \"../utils/resolver\";\nimport {Model} from \"../models/Model\";\n\nconst Comment = new Resolver(new Model('comment'));\n\nexport default  (app: Express) =\u003e {\n    // GET /api/comment/{id} gets a comment (assuming comment model exists) by id\n    app.get('/api/comment/:id', Comment.get)\n}\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsroehrl%2Fnode-express-typescript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsroehrl%2Fnode-express-typescript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsroehrl%2Fnode-express-typescript/lists"}