express-nodejs-app/README.md

140 lines
3.4 KiB
Markdown
Raw Permalink Normal View History

2021-09-13 17:31:39 +07:00
# Express Nodejs App
2021-09-13 20:28:43 +07:00
- Nodejs
- Express
- TypeScript
- Prettier
- ESLint
- Docker
- DroneCI
- Lerna Support
2021-09-14 09:50:25 +07:00
- Support decorators
2021-09-13 20:28:43 +07:00
# Quickstart
2021-09-14 11:07:08 +07:00
- Clone repository
```shell
git clone https://git.cubetiqs.com/CUBETIQ/express-nodejs-app.git --recurse-submodules --remote-submodules
```
2021-09-15 09:02:01 +07:00
- Update an existed repository
```shell
git submodule update --init --remote
```
- Add Packages
```shell
2021-09-16 18:18:13 +07:00
mkdir packages && git submodule add https://git.cubetiqs.com/CUBETIQ/ts-common.git packages/ts-common && git submodule add https://git.cubetiqs.com/CUBETIQ/cubetiq-express-server.git packages/cubetiq-express-server
2021-09-15 09:02:01 +07:00
```
2021-09-16 18:18:13 +07:00
- Install and Build Packages
2021-09-14 11:07:08 +07:00
```shell
2021-09-16 18:18:13 +07:00
yarn && npx lerna run build
2021-09-14 11:07:08 +07:00
```
2021-09-13 20:28:43 +07:00
- Start
2021-09-13 20:25:18 +07:00
```shell
yarn start
```
2021-09-13 20:28:43 +07:00
- Navigate
2021-09-13 20:25:18 +07:00
```shell
2021-09-14 09:50:25 +07:00
curl http://localhost:3000/info
2021-09-13 20:25:18 +07:00
```
2021-09-13 20:28:43 +07:00
- Response
2021-09-13 20:25:18 +07:00
```json
{
"startedAt": "2021-09-13T13:21:04.184Z",
"message": "Instance id: presenter#143470",
"status": "OK"
}
```
2021-09-13 20:28:43 +07:00
2021-09-16 09:55:36 +07:00
# Build application
```shell
yarn build
```
2021-09-14 09:50:25 +07:00
# Quick Register Controller
- Add new controller `src/controller/home.controller.ts`
```typescript
import { Request, Response } from "express"
2021-09-16 09:54:21 +07:00
import { Controller, Get, Post, Delete } from "../decorators"
2021-09-14 09:50:25 +07:00
const data: any[] = []
@Controller("/home")
export default class HomeController {
constructor() {}
@Get()
public get(req: Request, res: Response) {
res.json(data)
}
@Post()
public create(req: Request, res: Response) {
const body = req.body
if (body == null) {
return res.status(400).json({
status: 400,
message: "Data is required",
})
}
data.push(body)
res.json({
message: "Data created successfully",
body: body,
})
}
@Delete("/:id")
public get(req: Request, res: Response) {
const id = req.params.id
res.json({
id: id,
})
}
2021-09-14 09:50:25 +07:00
}
```
- Register controller into index.ts `(src/controllers/index.ts)`
```ts
import IndexController from "./index.controller"
import PersonController from "./person.controller"
import HomeController from "./home.controller"
export const controllers = [IndexController, PersonController, HomeController]
```
2021-09-14 11:07:08 +07:00
- Output registered routes
```ts
┌─────────┬──────────────────────┬─────────────────────────────────┐
│ (index) │ api │ handler │
├─────────┼──────────────────────┼─────────────────────────────────┤
│ 0 │ 'GET /hello' │ 'IndexController.index' │
│ 1 │ 'GET /hello/:name' │ 'IndexController.hello' │
│ 2 │ 'GET /person' │ 'PersonController.getPersons' │
│ 3 │ 'POST /person' │ 'PersonController.createPerson' │
│ 4 │ 'DELETE /person/:id' │ 'PersonController.deletePerson' │
└─────────┴──────────────────────┴─────────────────────────────────┘
```
2021-09-13 17:31:39 +07:00
### Contributors
2021-09-13 20:28:43 +07:00
2021-09-16 10:07:36 +07:00
- Sambo Chea <sombochea@cubetiqs.com> [4099C19544D3FF79]