-
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
1 parent
afa8734
commit 38ceaff
Showing
20 changed files
with
6,119 additions
and
54 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,35 @@ | ||
# compiled output | ||
/dist | ||
/node_modules | ||
|
||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
|
||
# OS | ||
.DS_Store | ||
|
||
# Tests | ||
/coverage | ||
/.nyc_output | ||
|
||
# IDEs and editors | ||
/.idea | ||
.project | ||
.classpath | ||
.c9/ | ||
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
|
||
# IDE - VSCode | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
development.env |
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 @@ | ||
{ | ||
"singleQuote": true, | ||
"trailingComma": "all" | ||
} |
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 |
---|---|---|
@@ -1,75 +1,59 @@ | ||
<p align="center"> | ||
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo_text.svg" width="320" alt="Nest Logo" /></a> | ||
</p> | ||
|
||
[travis-image]: https://api.travis-ci.org/nestjs/nest.svg?branch=master | ||
[travis-url]: https://travis-ci.org/nestjs/nest | ||
[linux-image]: https://img.shields.io/travis/nestjs/nest/master.svg?label=linux | ||
[linux-url]: https://travis-ci.org/nestjs/nest | ||
|
||
<p align="center">A progressive <a href="http://nodejs.org" target="blank">Node.js</a> framework for building efficient and scalable server-side applications, heavily inspired by <a href="https://angular.io" target="blank">Angular</a>.</p> | ||
<p align="center"> | ||
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/v/@nestjs/core.svg" alt="NPM Version" /></a> | ||
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/l/@nestjs/core.svg" alt="Package License" /></a> | ||
<a href="https://www.npmjs.com/~nestjscore"><img src="https://img.shields.io/npm/dm/@nestjs/core.svg" alt="NPM Downloads" /></a> | ||
<a href="https://travis-ci.org/nestjs/nest"><img src="https://api.travis-ci.org/nestjs/nest.svg?branch=master" alt="Travis" /></a> | ||
<a href="https://travis-ci.org/nestjs/nest"><img src="https://img.shields.io/travis/nestjs/nest/master.svg?label=linux" alt="Linux" /></a> | ||
<a href="https://coveralls.io/github/nestjs/nest?branch=master"><img src="https://coveralls.io/repos/github/nestjs/nest/badge.svg?branch=master#5" alt="Coverage" /></a> | ||
<a href="https://gitter.im/nestjs/nestjs?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=body_badge"><img src="https://badges.gitter.im/nestjs/nestjs.svg" alt="Gitter" /></a> | ||
<a href="https://opencollective.com/nest#backer"><img src="https://opencollective.com/nest/backers/badge.svg" alt="Backers on Open Collective" /></a> | ||
<a href="https://opencollective.com/nest#sponsor"><img src="https://opencollective.com/nest/sponsors/badge.svg" alt="Sponsors on Open Collective" /></a> | ||
<a href="https://paypal.me/kamilmysliwiec"><img src="https://img.shields.io/badge/Donate-PayPal-dc3d53.svg"/></a> | ||
<a href="https://twitter.com/nestframework"><img src="https://img.shields.io/twitter/follow/nestframework.svg?style=social&label=Follow"></a> | ||
</p> | ||
<!--[](https://opencollective.com/nest#backer) | ||
[](https://opencollective.com/nest#sponsor)--> | ||
|
||
## Description | ||
|
||
[Nest](https://github.com/nestjs/nest) framework TypeScript starter repository. | ||
|
||
## Installation | ||
# seu-xsc-webservice | ||
|
||
东南大学学工部学生处「网上学工」系统后端服务 | ||
|
||
**使用 [Nest.js](https://docs.nestjs.com) 构建** | ||
|
||
## 开始开发(手把手版) | ||
|
||
### 本地调试数据库配置 | ||
|
||
**1.以非认证模式启动 Mongod 服务** | ||
|
||
```bash | ||
$ npm install | ||
$ mongod --dbpath <数据保存位置> | ||
``` | ||
|
||
## Running the app | ||
**2.通过 MongoDB 客户端连接,建立数据库用户** | ||
|
||
```bash | ||
# development | ||
$ npm run start | ||
$ mongo | ||
``` | ||
|
||
# watch mode | ||
$ npm run start:dev | ||
建立数据库用户,此处数据库名称为 `xsc`,用户名为`user`,密码为 `pwd`: | ||
|
||
# production mode | ||
$ npm run start:prod | ||
``` | ||
> use xsc | ||
> db.createUser({user:'user', pwd:'pwd', roles:['dbAdmin']}) | ||
``` | ||
|
||
## Test | ||
**3.以认证模式重启 Mongod 服务** | ||
|
||
```bash | ||
# unit tests | ||
$ npm run test | ||
$ mongod --dbpath <数据保存位置(须和之前一致)> --auth | ||
``` | ||
|
||
# e2e tests | ||
$ npm run test:e2e | ||
### 创建本地配置文件 | ||
|
||
# test coverage | ||
$ npm run test:cov | ||
进入 `seu-xsc-webservice` 目录下: | ||
|
||
```bash | ||
$ cp development.example.env development.env | ||
``` | ||
|
||
## Support | ||
按照需要修改你的配置文件 development.env(请不要修改 development.example.env)。 | ||
|
||
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). | ||
### 安装依赖 | ||
|
||
## Stay in touch | ||
项目使用 yarn 作为依赖管理工具,请首先自行安装 yarn。 | ||
|
||
- Author - [Kamil Myśliwiec](https://kamilmysliwiec.com) | ||
- Website - [https://nestjs.com](https://nestjs.com/) | ||
- Twitter - [@nestframework](https://twitter.com/nestframework) | ||
```bash | ||
$ yarn | ||
``` | ||
|
||
## License | ||
### 开始调试 | ||
|
||
```bash | ||
$ yarn dev | ||
``` | ||
|
||
Nest is [MIT licensed](LICENSE). |
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,3 @@ | ||
NODE_ENV = development | ||
PORT = 3000 | ||
DB_URL = mongo://user:pwd@localhost:27017/xsc |
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 @@ | ||
{ | ||
"collection": "@nestjs/schematics", | ||
"sourceRoot": "src" | ||
} |
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,69 @@ | ||
{ | ||
"name": "seu-xsc-webservice", | ||
"version": "0.0.1", | ||
"description": "", | ||
"author": "", | ||
"license": "MIT", | ||
"scripts": { | ||
"prebuild": "rimraf dist", | ||
"build": "nest build", | ||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", | ||
"dev":"nest start --watch", | ||
"start": "nest start", | ||
"start:dev": "nest start --watch", | ||
"start:debug": "nest start --debug --watch", | ||
"start:prod": "node dist/main", | ||
"lint": "tslint -p tsconfig.json -c tslint.json", | ||
"test": "jest", | ||
"test:watch": "jest --watch", | ||
"test:cov": "jest --coverage", | ||
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand", | ||
"test:e2e": "jest --config ./test/jest-e2e.json" | ||
}, | ||
"dependencies": { | ||
"@hapi/joi": "^16.1.8", | ||
"@nestjs/common": "^6.7.2", | ||
"@nestjs/core": "^6.7.2", | ||
"@nestjs/mongoose": "^6.1.2", | ||
"@nestjs/platform-express": "^6.7.2", | ||
"dotenv": "^8.2.0", | ||
"mongoose": "^5.7.12", | ||
"reflect-metadata": "^0.1.13", | ||
"rimraf": "^3.0.0", | ||
"rxjs": "^6.5.3" | ||
}, | ||
"devDependencies": { | ||
"@nestjs/cli": "^6.9.0", | ||
"@nestjs/schematics": "^6.7.0", | ||
"@nestjs/testing": "^6.7.1", | ||
"@types/dotenv": "^8.2.0", | ||
"@types/express": "^4.17.1", | ||
"@types/hapi__joi": "^16.0.3", | ||
"@types/jest": "^24.0.18", | ||
"@types/node": "^12.7.5", | ||
"@types/supertest": "^2.0.8", | ||
"jest": "^24.9.0", | ||
"prettier": "^1.18.2", | ||
"supertest": "^4.0.2", | ||
"ts-jest": "^24.1.0", | ||
"ts-loader": "^6.1.1", | ||
"ts-node": "^8.4.1", | ||
"tsconfig-paths": "^3.9.0", | ||
"tslint": "^5.20.0", | ||
"typescript": "^3.6.3" | ||
}, | ||
"jest": { | ||
"moduleFileExtensions": [ | ||
"js", | ||
"json", | ||
"ts" | ||
], | ||
"rootDir": "src", | ||
"testRegex": ".spec.ts$", | ||
"transform": { | ||
"^.+\\.(t|j)s$": "ts-jest" | ||
}, | ||
"coverageDirectory": "./coverage", | ||
"testEnvironment": "node" | ||
} | ||
} |
Empty file.
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,22 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
|
||
describe('AppController', () => { | ||
let appController: AppController; | ||
|
||
beforeEach(async () => { | ||
const app: TestingModule = await Test.createTestingModule({ | ||
controllers: [AppController], | ||
providers: [AppService], | ||
}).compile(); | ||
|
||
appController = app.get<AppController>(AppController); | ||
}); | ||
|
||
describe('root', () => { | ||
it('should return "Hello World!"', () => { | ||
expect(appController.getHello()).toBe('Hello World!'); | ||
}); | ||
}); | ||
}); |
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,12 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { AppService } from './app.service'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
|
||
@Get() | ||
getHello(): string { | ||
return this.appService.getHello(); | ||
} | ||
} |
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,23 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
import { ConfigModule } from './config/config.module'; | ||
import { ConfigService } from './config/config.service'; | ||
import { MongooseModule } from '@nestjs/mongoose'; | ||
|
||
const mongooseModule = MongooseModule.forRootAsync({ | ||
imports: [ConfigModule], | ||
useFactory: async (configService: ConfigService) => ({ | ||
uri: configService.mongodbUri, | ||
useNewUrlParser: true, | ||
useUnifiedTopology: true | ||
}), | ||
inject: [ConfigService], | ||
}); | ||
|
||
@Module({ | ||
imports: [ConfigModule, mongooseModule], | ||
controllers: [AppController], | ||
providers: [AppService], | ||
}) | ||
export class AppModule {} |
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,8 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
getHello(): string { | ||
return 'Hello World!'; | ||
} | ||
} |
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,14 @@ | ||
import { Module, Global } from '@nestjs/common'; | ||
import { ConfigService } from './config.service'; | ||
|
||
@Global() | ||
@Module({ | ||
providers: [ | ||
{ | ||
provide: ConfigService, | ||
useValue: new ConfigService(`${process.env.NODE_ENV || 'development'}.env`), | ||
}, | ||
], | ||
exports: [ConfigService], | ||
}) | ||
export class ConfigModule {} |
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,40 @@ | ||
import * as dotenv from 'dotenv'; | ||
import * as Joi from '@hapi/joi'; | ||
import * as fs from 'fs'; | ||
|
||
export type EnvConfig = Record<string, string>; | ||
|
||
export class ConfigService { | ||
private readonly envConfig: EnvConfig; | ||
|
||
constructor(filePath: string) { | ||
const config = dotenv.parse(fs.readFileSync(filePath)); | ||
this.envConfig = this.validateInput(config); | ||
} | ||
|
||
/** | ||
* Ensures all needed variables are set, and returns the validated JavaScript object | ||
* including the applied default values. | ||
*/ | ||
private validateInput(envConfig: EnvConfig): EnvConfig { | ||
const envVarsSchema: Joi.ObjectSchema = Joi.object({ | ||
NODE_ENV: Joi.string() | ||
.valid('development', 'production', 'test', 'provision') | ||
.default('development'), | ||
PORT: Joi.number().default(3000), | ||
MONGODB_URI: Joi.string().required() | ||
}); | ||
|
||
const { error, value: validatedEnvConfig } = envVarsSchema.validate( | ||
envConfig, | ||
); | ||
if (error) { | ||
throw new Error(`❌配置验证出错,请检查配置文件: ${error.message}`); | ||
} | ||
return validatedEnvConfig; | ||
} | ||
|
||
get mongodbUri(): string { | ||
return String(this.envConfig.MONGODB_URI); | ||
} | ||
} |
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,8 @@ | ||
import { NestFactory } from '@nestjs/core'; | ||
import { AppModule } from './app.module'; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create(AppModule); | ||
await app.listen(3000); | ||
} | ||
bootstrap(); |
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,23 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import * as request from 'supertest'; | ||
import { AppModule } from './../src/app.module'; | ||
|
||
describe('AppController (e2e)', () => { | ||
let app; | ||
|
||
beforeEach(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
imports: [AppModule], | ||
}).compile(); | ||
|
||
app = moduleFixture.createNestApplication(); | ||
await app.init(); | ||
}); | ||
|
||
it('/ (GET)', () => { | ||
return request(app.getHttpServer()) | ||
.get('/') | ||
.expect(200) | ||
.expect('Hello World!'); | ||
}); | ||
}); |
Oops, something went wrong.