Backend boilerplate codes for developing backend by typescript as fast as light!
Default database package is currently Mongoose
You must modify your code on your own to execute without mongoose.
git clone --depth 1 --single-branch https://github.com/WebBoilerplates/Typescript-Node-Express-Mongodb-backend "Your project name"
Before of all, Please check the document of Express-Quick-Builder, the main library of this template repository.
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.
- 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.
See express-quick-builder document
-
Place your socket event handler file on src/io/routes
foo.socket.ts
orbar.rawsocket.ts
-
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.
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.
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.
Use QuickCert.js. It is not only easy, but also blazing fast and secure.
returnError(
ERROR_MESSAGE,
HTTP_ERROR_CODE_NUMBER,
ERROR_REASON_STRING,
OPTIONS,
);
class Foo {
private getFoo = C.Wrapper(async (req, res) => {
throw C.error.fooError();
});
}
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 error from '@error/index';
...
// 404
app.use(req => {
error.pageNotFound(req.url);
});
Create .env file on your project's root directory.
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
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
- 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.
All code belongs to HADMARINE. You can use this code as Apache 2.0 License.
Copyright 2020 HADMARINE, All rights reserved.