Skip to content

HADMARINE/quick-nodejs-backend

Repository files navigation

Typescript-Node-Express-Mongodb-backend

Readme for other languages

Description

Backend boilerplate codes for developing backend by typescript as fast as light!

Informations

Default database package is currently Mongoose
You must modify your code on your own to execute without mongoose.

Cloning

git clone --depth 1 --single-branch https://github.com/WebBoilerplates/Typescript-Node-Express-Mongodb-backend "Your project name"

How to use

Before of all, Please check the document of Express-Quick-Builder, the main library of this template repository.

1. HTTP Router (Express based)

Place your file in routes like : /routes/YOUR_ROUTE/index.controller/router.ts
Then automated code will route your files :)

Warning! If the filename is not like *.routes.ts or *.controller.ts , auto router will not detect your file.

Differences of _.controller.ts and _.routes.ts

  • controller.ts is controlled router, your code will be guided by pre-written codes.
  • routes.ts is uncontrolled router, export default express requestHandler function to execute.
Usage

See express-quick-builder document

2. Socket Event Handler

  1. Place your socket event handler file on src/io/routes
    foo.socket.ts or bar.rawsocket.ts

  2. Write your boilerplate code

  • *.socket.ts
import SC from '@lib/blueprint/SocketController';

export default class extends SC {
  main() {
    //your code goes here
    //this.parameters - parameters of current event
    //this.global - socketio.Server (global io)
    //this.current - socketio.Socket (current socket event handler)
  }
}

strict event handler will be auto assigned by file name (filename.socket.ts -> filename event listener)

  • *.rawsocket.ts
export default function (io: Socketio.Server, socket: Socketio.Socket) {
  //your code goes here
}

raw event handler will not assign events automatically.

3. Commands

Execute yarn dev on your command line.
If the port (default port is 4000) is already in use, it will try to listen on another port automatically.
If you don't want this function, set PORT_STRICT on your .env to true.

Error Handling (returnError.ts)

We made some boilerplate errors on ./src/error/ErrorDictionary.ts.
We suggest to use this method because if you create your own error every time,
integrity of error datas would be broken and can cause side-effects.
So, if you can, declare error on ./src/error/ErrorDictionary.ts and use.

4. Certificate Management

Use QuickCert.js. It is not only easy, but also blazing fast and secure.

How to create error

First, declare error on src/error file.

returnError(
  ERROR_MESSAGE,
  HTTP_ERROR_CODE_NUMBER,
  ERROR_REASON_STRING,
  OPTIONS,
);

Next, throw your error on routing files.

class Foo {
  private getFoo = C.Wrapper(async (req, res) => {
    throw C.error.fooError();
  });
}
Description

ERROR_MESSAGE is string type, it will explain why the error has occured.
HTTP_ERROR_CODE_NUMBER is number type, it will return HTTP Status code.
ERROR_REASON_STRING is string type, you may use this data for managing exceptions.
OPTIONS is Object type. You can give factors below. It is not a required factor.

{
  "log": true
} //Will print log on console.
Import and usage
import error from '@error/index';
...
// 404
app.use(req => {
  error.pageNotFound(req.url);
});

4. env(dotenv)

Create .env file on your project's root directory.

Required Factors

DB_HOST : mongodb host (You must not enter db name)
DB_NAME : mongodb database name
DB_USER : mongodb user
DB_PASS : mongodb password
REQUEST_URI : URI that your client will access. If you don't set your domain, cors origin uri will set to * (wildcard) and cannot protect your api. If you want to set your origin uri to a wildcard, set to * or if will occur warning log.
TOKEN_KEY : JWT Token private key

Not Required Factors

PORT : Port that server app will run
PORT_STRICT : Set to true if you don't want to use auto port-detection and use only your own port.
EXAMINE_PASSWORD : parameter whether double-check password encryption

ETC

Known Issues

  • Nodemon will not run properly at version 2.0.3 and above at WIN32 (windows). Downgraded currently to 2.0.2 until fixed.
  • Currently have plan to improve SocketEventHandlers and migrate to express-quick-builder or create new open source package.
  • Test code coverage is currently low, Need to improve above 90 percent.

License

All code belongs to HADMARINE. You can use this code as Apache 2.0 License.

See details of License

Copyright 2020 HADMARINE, All rights reserved.