forked from chenlong-io/ts-koa-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
AlwaysVe
committed
May 10, 2019
0 parents
commit f380b4f
Showing
12 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
dist | ||
yarn.lock | ||
package.json.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# ts-koa-starter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const { name } = require('./package.json'); | ||
const path = require('path'); | ||
|
||
console.log(name); | ||
|
||
module.exports = { | ||
apps: [ | ||
{ | ||
name, | ||
script: path.resolve(__dirname, './dist/index.js'), | ||
args: 'one two', | ||
instances: require('os').cpus().length, | ||
autorestart: true, | ||
watch: true, | ||
max_memory_restart: '1G', | ||
env: { | ||
NODE_ENV: 'development', | ||
PORT: 3000 | ||
}, | ||
env_production: { | ||
NODE_ENV: 'production', | ||
PORT: 8080 | ||
} | ||
} | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"watch": ["src"], | ||
"ext": "ts", | ||
"exec": "ts-node -r tsconfig-paths/register src/index.ts" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const env = process.env.NODE_ENV; | ||
|
||
module.exports = { | ||
type: 'mysql', | ||
host: '47.110.130.242', | ||
port: 3306, | ||
username: 'vcontroller', | ||
password: 'te85jRPErW6JiNke', | ||
database: 'vcontroller', | ||
synchronize: true, | ||
logging: false, | ||
entities: [`${env == 'dev' ? 'src' : 'dist'}/entity/*{.ts,.js}`] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "ts-koa-starter", | ||
"version": "1.0.0", | ||
"description": "一个简单的koa+typescript+typeorm的起手式", | ||
"tags": [ | ||
"orm", | ||
"typescript", | ||
"typeorm", | ||
"koa" | ||
], | ||
"scripts": { | ||
"start": "NODE_ENV=dev nodemon", | ||
"build": "rm -rf dist && tsc", | ||
"pro": "node_modules/.bin/pm2 start --env=production", | ||
"stop": "node_modules/.bin/pm2 stop all" | ||
}, | ||
"dependencies": { | ||
"koa": "^2.7.0", | ||
"koa-bodyparser": "^4.2.1", | ||
"koa-router": "^7.4.0", | ||
"mysql": "^2.17.1", | ||
"typeorm": "^0.2.17" | ||
}, | ||
"devDependencies": { | ||
"@types/koa": "^2.0.48", | ||
"@types/koa-bodyparser": "^4.2.2", | ||
"@types/koa-router": "^7.0.40", | ||
"@types/node": "^12.0.0", | ||
"nodemon": "^1.19.0", | ||
"pm2": "^3.5.0", | ||
"ts-node": "^8.1.0", | ||
"tsconfig-paths": "^3.8.0", | ||
"typescript": "^3.4.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import HomeService from '../service/home-service'; | ||
|
||
export default class HomeController { | ||
static async hello(ctx) { | ||
ctx.body = await HomeService.hello(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm'; | ||
|
||
@Entity() | ||
export default class Category { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import 'reflect-metadata'; | ||
import * as Koa from 'koa'; | ||
import { createConnection } from 'typeorm'; | ||
import * as Router from 'koa-router'; | ||
import * as bodyParser from 'koa-bodyparser'; | ||
import AppRoutes from './routes'; | ||
|
||
createConnection() | ||
.then(async connection => { | ||
// create koa app | ||
const app = new Koa(); | ||
const router = new Router(); | ||
const port = process.env.PORT || 3000; | ||
|
||
// register all application routes | ||
AppRoutes.forEach(route => router[route.method](route.path, route.action)); | ||
|
||
app.use(bodyParser()); | ||
app.use(router.routes()); | ||
app.use(router.allowedMethods()); | ||
app.listen(port); | ||
|
||
console.log(`应用启动成功 端口:${port}`); | ||
}) | ||
.catch(error => console.log('TypeORM 链接失败: ', error)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import HomeController from './controller/home-controller'; | ||
|
||
export default [ | ||
{ | ||
path: '/', | ||
method: 'get', | ||
action: HomeController.hello | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Context } from 'koa'; | ||
import { getManager } from 'typeorm'; | ||
import Category from '../entity/category'; | ||
|
||
export default class HomeService { | ||
static async hello(context?: Context) { | ||
const categoryRepository = getManager().getRepository(Category); | ||
const newCategory = categoryRepository.create({ | ||
name: '陈龙' | ||
}); | ||
await categoryRepository.save(newCategory); | ||
|
||
return 'hello'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"declaration": false, | ||
"removeComments": true, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"target": "es6", | ||
"sourceMap": false, | ||
"outDir": "./dist", | ||
"baseUrl": "./" | ||
}, | ||
"exclude": ["node_modules"] | ||
// "include": ["ormconfig.json"] | ||
} |