Compare commits

..

28 Commits

Author SHA1 Message Date
c999b4a8f6 Fixed dockerfile build missing yarn
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-16 18:25:35 +07:00
7a2c3dec97 Fixed lerna build
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-16 18:25:11 +07:00
9ef8db077f Forgot yarn command
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-16 18:20:52 +07:00
da0235c0b3 Fixed drone ci yarn command failed
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-16 18:20:09 +07:00
0378f270fc Updated readme
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-16 18:18:13 +07:00
03458fe00c Add express server from module and updated the server and config
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-16 18:14:49 +07:00
c41002a96b Add signed fringerprint for contr 2021-09-16 10:07:36 +07:00
00eedce6ea Updated readme
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-16 09:55:36 +07:00
Sambo Chea
84656ec61a Update README.md 2021-09-16 09:54:21 +07:00
dd445a22c0 Task: Fixed and updated for export all functions for decorators
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-15 20:17:30 +07:00
923e08949b Updated guide for clone package
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-15 09:02:01 +07:00
70f0ef93f8 Task: Fixed package json for yarn serve and fixed tsconfig for build with all packages to src only
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-15 08:52:20 +07:00
1dfad5d122 Task: Fixed and changed to yarn script in Dockerfile
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-15 08:36:12 +07:00
744c6585bb Updated readme
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-14 11:07:08 +07:00
a00c5b0650 Task: Updated the delete route and controller
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-14 10:51:24 +07:00
5120752ecb Updated readme and quide
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-14 09:50:25 +07:00
20fa35a575 Task: Fixed controller and application for routes and allow middleware for json and form urclosed
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-14 09:44:25 +07:00
67c94211e7 Task: Add reflection metadata for decorators and add basic implements for controller and express application and constants and app
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-14 09:06:03 +07:00
a6546b7676 Task: Add application instance for express and nodejs project 2021-09-14 08:31:08 +07:00
c7f3aa02b5 Updated package and gitignore
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-13 20:53:05 +07:00
9af0a6728e Add server port in env
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-13 20:50:53 +07:00
75f0d3a092 Updated and build for ts common
All checks were successful
continuous-integration/drone/push Build is passing
2021-09-13 20:49:05 +07:00
28e7cce904 Fixed yarn build for ts common inside the packages
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-13 20:47:45 +07:00
4ce24d1e30 Fixed clone submodules for drone build
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-13 20:42:43 +07:00
c095086d4f Replaced by yarn of npm in Dockerfile
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-13 20:39:08 +07:00
c47221cb13 Add clone depth 50
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-13 20:38:17 +07:00
e0f16081b0 Updated husky and install for prettier
Some checks failed
continuous-integration/drone/push Build is failing
2021-09-13 20:28:43 +07:00
ce813262c3 Updated readme 2021-09-13 20:25:18 +07:00
17 changed files with 305 additions and 68 deletions

View File

@@ -1,10 +1,14 @@
kind: pipeline kind: pipeline
type: docker type: docker
name: ci name: ci
steps: steps:
- name: submodules
image: d.ctdn.net/alpine/git
commands:
- git submodule update --init --recursive
- name: build - name: build
image: d.ctdn.net/node image: d.ctdn.net/node
commands: commands:
- npm config set registry https://r.ctdn.net - yarn config set registry https://r.ctdn.net
- npm install - yarn && yarn build
- npm run build

1
.env
View File

@@ -0,0 +1 @@
SERVER_PORT=3000

2
.gitignore vendored
View File

@@ -118,5 +118,7 @@ dist
.yarn/install-state.gz .yarn/install-state.gz
.pnp.* .pnp.*
.DS_Store
package-lock.json package-lock.json
yarn.lock yarn.lock

3
.gitmodules vendored
View File

@@ -1,3 +1,6 @@
[submodule "packages/ts-common"] [submodule "packages/ts-common"]
path = packages/ts-common path = packages/ts-common
url = https://git.cubetiqs.com/CUBETIQ/ts-common.git url = https://git.cubetiqs.com/CUBETIQ/ts-common.git
[submodule "packages/cubetiq-express-server"]
path = packages/cubetiq-express-server
url = https://git.cubetiqs.com/CUBETIQ/cubetiq-express-server.git

View File

@@ -13,10 +13,8 @@ RUN apk update && \
WORKDIR /app WORKDIR /app
COPY . /app COPY . /app
RUN npm config set registry https://r.ctdn.net RUN yarn config set registry https://r.ctdn.net
RUN npm config get registry RUN yarn && yarn build
RUN npm install
RUN npm run build
EXPOSE 3000 EXPOSE 3000
CMD [ "npm" , "serve"] CMD [ "yarn" , "serve"]

145
README.md
View File

@@ -1,12 +1,139 @@
# Express Nodejs App # Express Nodejs App
- Nodejs
- Express - Nodejs
- TypeScript - Express
- Prettier - TypeScript
- ESLint - Prettier
- Docker - ESLint
- DroneCI - Docker
- Lerna Support - DroneCI
- Lerna Support
- Support decorators
# Quickstart
- Clone repository
```shell
git clone https://git.cubetiqs.com/CUBETIQ/express-nodejs-app.git --recurse-submodules --remote-submodules
```
- Update an existed repository
```shell
git submodule update --init --remote
```
- Add Packages
```shell
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
```
- Install and Build Packages
```shell
yarn && npx lerna run build
```
- Start
```shell
yarn start
```
- Navigate
```shell
curl http://localhost:3000/info
```
- Response
```json
{
"startedAt": "2021-09-13T13:21:04.184Z",
"message": "Instance id: presenter#143470",
"status": "OK"
}
```
# Build application
```shell
yarn build
```
# Quick Register Controller
- Add new controller `src/controller/home.controller.ts`
```typescript
import { Request, Response } from "express"
import { Controller, Get, Post, Delete } from "../decorators"
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,
})
}
}
```
- 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]
```
- 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'
```
### Contributors ### Contributors
- Sambo Chea <sombochea@cubetiqs.com>
- Sambo Chea <sombochea@cubetiqs.com> [4099C19544D3FF79]

View File

@@ -1,15 +1,16 @@
{ {
"name": "@cubetiq/express-nodejs-app", "name": "@cubetiq/express-nodejs-app",
"version": "1.0.0", "version": "1.0.0",
"description": "Express, Nodejs application for service", "description": "Express, Nodejs application for micro-service",
"main": "dist/app.js", "main": "dist/app.js",
"private": true, "private": true,
"scripts": { "scripts": {
"start": "ts-node-dev --respawn --transpile-only src/app.ts", "start": "ts-node-dev --respawn --transpile-only src/app.ts",
"build": "rm -rf dist && tsc", "build": "lerna run build && rm -rf dist && tsc",
"serve": "yarn run build && node dist/app.js", "serve": "yarn build && node dist/app.js",
"bootstrap": "lerna bootstrap", "bootstrap": "lerna bootstrap",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1",
"prepare": "husky install"
}, },
"repository": { "repository": {
"type": "git", "type": "git",
@@ -17,12 +18,14 @@
}, },
"keywords": [ "keywords": [
"Express", "Express",
"Nodejs" "Nodejs",
"Typescript"
], ],
"author": "Sambo Chea <sombochea@cubetiqs.com>", "author": "Sambo Chea <sombochea@cubetiqs.com>",
"license": "ISC", "license": "ISC",
"devDependencies": { "devDependencies": {
"@types/express": "^4.17.13", "@types/express": "^4.17.13",
"@types/node": "^16.9.1",
"@typescript-eslint/eslint-plugin": "^4.30.0", "@typescript-eslint/eslint-plugin": "^4.30.0",
"@typescript-eslint/parser": "^4.30.0", "@typescript-eslint/parser": "^4.30.0",
"dotenv": "^10.0.0", "dotenv": "^10.0.0",
@@ -35,12 +38,12 @@
"lint-staged": "^11.1.2", "lint-staged": "^11.1.2",
"prettier": "2.3.2", "prettier": "2.3.2",
"ts-node-dev": "^1.1.8", "ts-node-dev": "^1.1.8",
"typescript": "^4.4.2", "typescript": "^4.4.2"
"@types/node": "^16.9.1"
}, },
"dependencies": { "dependencies": {
"express": "^4.17.1", "@cubetiq/ts-common": "1.0.0",
"@cubetiq/ts-common": "1.0.0" "@cubetiq/express-server": "1.0.0",
"express": "^4.17.1"
}, },
"lint-staged": { "lint-staged": {
"**/*": "prettier --write --ignore-unknown" "**/*": "prettier --write --ignore-unknown"

View File

@@ -1,5 +1,28 @@
// app config // app config
import "./dotenv" import "./dotenv"
// core app import { createServer } from "http"
import "./server" import { SERVER_PORT } from "./app.config"
import { info } from "@cubetiq/ts-common/dist/log"
import application from "./server"
// get current host id
const hostId = `${require("os").hostname()}#${process.pid}`
const startedAt = new Date()
const app = application.instance
const httpServer = createServer(app)
info(
`Application server running on: ${hostId} at port: ${SERVER_PORT} and started at: ${startedAt}`
)
httpServer.listen(SERVER_PORT)
app.get("/info", (req, res) => {
res.type("json")
res.send({
startedAt,
message: `Instance id: ${hostId}`,
status: "OK",
})
})

View File

@@ -0,0 +1,17 @@
import { Request, Response } from "express"
import { Controller, Get } from "@cubetiq/express-server/dist/decorators"
@Controller("/hello")
export default class IndexController {
constructor() {}
@Get("")
public index(req: Request, res: Response) {
res.send(`Hello Index`)
}
@Get("/:name")
public hello(req: Request, res: Response) {
res.send(`Hello ${req.params.name}`)
}
}

4
src/controller/index.ts Normal file
View File

@@ -0,0 +1,4 @@
import IndexController from "./index.controller"
import PersonController from "./person.controller"
export const controllers = [IndexController, PersonController]

View File

@@ -0,0 +1,67 @@
import { Request, Response } from "express"
import {
Controller,
Delete,
Get,
Post,
} from "@cubetiq/express-server/dist/decorators"
const persons: Array<any> = [
{
id: 1,
name: "Sambo",
},
]
@Controller("/person")
export default class PersonController {
@Get()
public getPersons(req: Request, res: Response) {
res.json(persons)
}
@Post()
public createPerson(req: Request, res: Response) {
const person = req.body
if (person == null) {
return res.status(400).json({
status: 400,
message: "Person is required",
})
}
persons.push(person)
res.json({
message: "Person created successfully",
body: person,
})
}
@Delete("/:id")
public deletePerson(req: Request, res: Response) {
const id = req.params.id
if (id == null) {
return res.status(400).json({
status: 400,
message: "Id is required",
})
}
const person = persons.find((person) => person.id === Number(id))
if (person == null) {
return res.status(404).json({
status: 404,
message: "Person not found",
})
}
persons.splice(persons.indexOf(person), 1)
res.json({
message: "Person deleted successfully",
body: person,
})
}
}

View File

@@ -1,25 +1,9 @@
import { createServer } from "http" import Application from "@cubetiq/express-server/dist"
import Express from "express" import { controllers as AllControllers } from "./controller"
import { SERVER_PORT } from "./app.config" class Server extends Application {
import { info } from "@cubetiq/ts-common/dist/log" get controllers(): any[] {
return AllControllers
}
}
// get current host id export default new Server()
const hostId = `${require("os").hostname()}#${process.pid}`
const startedAt = new Date()
const app = Express()
const httpServer = createServer(app)
info(
`Application server running on: ${hostId} at port: ${SERVER_PORT} and started at: ${startedAt}`
)
httpServer.listen(SERVER_PORT)
app.get("/", (req, res) => {
res.type("json")
res.send({
startedAt,
message: `Instance id: ${hostId}`,
status: "OK",
})
})

1
src/utils/utils.ts Normal file
View File

@@ -0,0 +1 @@
// nothing here for now

View File

@@ -1,15 +1,17 @@
{ {
"compilerOptions": { "compilerOptions": {
"target": "es5", "target": "es5",
"module": "commonjs", "module": "commonjs",
"moduleResolution": "node", "moduleResolution": "node",
"sourceMap": true, "sourceMap": false,
"outDir": "dist", "outDir": "dist",
"esModuleInterop": true, "esModuleInterop": true,
"forceConsistentCasingInFileNames": true, "forceConsistentCasingInFileNames": true,
"strict": true, "strict": true,
"lib": ["ES2015"], "lib": ["ES2015"],
"skipLibCheck": true "skipLibCheck": true,
}, "experimentalDecorators": true
"exclude": ["node_modules"] },
"include": ["src/**/*"],
"exclude": ["node_modules"]
} }