Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
AlwaysVe committed May 10, 2019
0 parents commit f380b4f
Show file tree
Hide file tree
Showing 12 changed files with 165 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
node_modules
dist
yarn.lock
package.json.lock
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ts-koa-starter
26 changes: 26 additions & 0 deletions ecosystem.config.js
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
}
}
]
};
5 changes: 5 additions & 0 deletions nodemon.json
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"
}
13 changes: 13 additions & 0 deletions ormconfig.js
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}`]
};
35 changes: 35 additions & 0 deletions package.json
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"
}
}
7 changes: 7 additions & 0 deletions src/controller/home-controller.ts
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();
}
}
10 changes: 10 additions & 0 deletions src/entity/category.ts
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;
}
25 changes: 25 additions & 0 deletions src/index.ts
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));
9 changes: 9 additions & 0 deletions src/routes.ts
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
}
];
15 changes: 15 additions & 0 deletions src/service/home-service.ts
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';
}
}
15 changes: 15 additions & 0 deletions tsconfig.json
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"]
}

0 comments on commit f380b4f

Please sign in to comment.