|
| 1 | +<img alt='overnightjs' src='https://github.com/seanpmaxwell/express-generator-typescript/raw/master/express-typescript.png' border='0'> |
| 2 | + |
| 3 | +[Express](https://www.npmjs.com/package/express) with [TypeScript's](https://www.npmjs.com/package/typescript) application generator. |
| 4 | + |
| 5 | +<a href="https://www.npmjs.com/package/express-generator-typescript" target="_blank"><img src="https://img.shields.io/npm/v/express-generator-typescript.svg" alt="NPM Version" /></a> |
| 6 | +<a href="https://www.npmjs.com/package/express-generator-typescript" target="_blank"><img src="https://img.shields.io/npm/l/express-generator-typescript.svg" alt="Package License" /></a> |
| 7 | +<a href="https://www.npmjs.com/package/express-generator-typescript" target="_blank"><img src="https://img.shields.io/npm/dm/express-generator-typescript.svg" alt="NPM Downloads" /></a> |
| 8 | + |
| 9 | + |
| 10 | +## What is it? |
| 11 | + |
| 12 | +Creates a new express application similar to the _express-generator_ module. Except this new |
| 13 | +application is configured to use TypeScript instead of plain JavaScript. |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +## Why express-generator-typescript? |
| 18 | + |
| 19 | +NodeJS is great for the rapid development of web-projects, but is often neglected because of the lack of |
| 20 | +type safety. TypeScript solves this issue and (along with its linter file) can even make your code |
| 21 | +more robust than some other static languages like Java. |
| 22 | + |
| 23 | +There are some other tools out there to generate express apps with TypeScript such as |
| 24 | +_express-generator-ts_, but these either haven't been updated in a while or install a lot of junk |
| 25 | +in your project (such as an ORM). |
| 26 | + |
| 27 | +Due to the heavy use of single-page-applications, no view-engine is configured by default. Express is |
| 28 | +only setup with the minimal settings for calling APIs and serving an index.html file. All the tools you |
| 29 | +need to run for development (while restarting on changes), building, testing, and running for production |
| 30 | +are packaged with this library. |
| 31 | + |
| 32 | +In addition, relative paths are also setup, so you don't have to go through the trouble of installing |
| 33 | +and configuring _tsconfig-paths_ and _module-alias_. Just make sure to update `paths` in _tsconfig.json_ |
| 34 | +and `_moduleAliases` in _package.json_ if you want to add/edit the relative paths. |
| 35 | + |
| 36 | + |
| 37 | +## Sample-project |
| 38 | + |
| 39 | +When you run _express-generator-typescript_, it sets up a very simple application with routes for |
| 40 | +adding, updating, deleting, and fetching user objects. This is just to demonstrate how routing is done |
| 41 | +with express. |
| 42 | + |
| 43 | +If you want a fully-secure application, you can pass the `--with-auth` option and you will have an |
| 44 | +application which requires you to logon before calling APIs on user objects. The app is |
| 45 | +configured with production quality client-side security and uses signed-cookies and jsonwebtokens |
| 46 | +to store user-session data. If you're new to web-development and still learning about securing websites, |
| 47 | +I highly encourage to use this option. |
| 48 | + |
| 49 | + |
| 50 | +## Installation |
| 51 | + |
| 52 | +```sh |
| 53 | +$ Just use 'npx' |
| 54 | + Or |
| 55 | +$ npm install -g express-generator-typescript |
| 56 | +``` |
| 57 | + |
| 58 | + |
| 59 | +## Quick Start |
| 60 | + |
| 61 | +The quickest way to get started is use npx and pass in the name of the project you want to create. |
| 62 | +If you don't specify a project name, the default _express-gen-ts_ will be used instead. |
| 63 | + |
| 64 | +Create the app: |
| 65 | + |
| 66 | +```bash |
| 67 | +$ npx express-generator-typescript "project name (default is express-gen-ts)" |
| 68 | +OR |
| 69 | +$ npx express-generator-typescript --with-auth "project name (default is express-gen-ts)" |
| 70 | +``` |
| 71 | + |
| 72 | +Start your express-generator-typescript app in development mode at `http://localhost:3000/`: |
| 73 | + |
| 74 | +```bash |
| 75 | +$ cd "project name" && npm run start:dev |
| 76 | +``` |
| 77 | + |
| 78 | + |
| 79 | +## Available commands for the server. |
| 80 | + |
| 81 | +- Run the server in development mode: `npm run start:dev`. |
| 82 | +- Run all unit-tests: `npm test`. |
| 83 | +- Run a single unit-test: `npm test -- --testFile="name of test file" (i.e. --testFile=Users)`. |
| 84 | +- Check for linting errors: `npm run lint`. |
| 85 | +- Build the project for production: `npm run build`. |
| 86 | +- Run the production build: `npm start`. |
| 87 | +- Run production build with a different env file `npm start -- --env="name of env file" (default is production)`. |
| 88 | + |
| 89 | + |
| 90 | +## Debugging |
| 91 | +During development, _express-generator-typescript_ uses `nodemon` to restart the server when changes |
| 92 | +are detected. If you want to enable debugging for node, you'll need to modify the nodemon configurations. |
| 93 | +This is located under `nodemonConfig:` in `package.json` for the server and `./spec/nodemon.json` for |
| 94 | +unit-testing. For the `exec` property, replace `ts-node` with `node --inspect -r ts-node/register`. |
| 95 | + |
| 96 | + |
| 97 | +## Note for windows users |
| 98 | + |
| 99 | +If you use the `--with-auth` option and are on Windows, the `bcrypt` module tends to be fussy. To |
| 100 | +use this module on Windows you need to make sure you have the node Windows build tools installed. |
| 101 | +I don't want to post instructions because they might change frequently. I would search the Microsoft |
| 102 | +docs on how to setup Node for Windows. To be able to debug in VSCODE on windows I also had to install |
| 103 | +the `node-gyp` module globally as well. |
| 104 | + |
| 105 | +Happy web-deving :) |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +## License |
| 110 | + |
| 111 | +[MIT](LICENSE) |
0 commit comments