add config

This commit is contained in:
iVamp 2023-10-24 13:25:21 +08:00
parent ac1f42cdce
commit d5c8f3ff52
5 changed files with 23 additions and 4 deletions

6
.env.example Normal file
View File

@ -0,0 +1,6 @@
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=auth
DB_USERNAME=root
DB_PASSWORD=

15
main.ts
View File

@ -1,8 +1,21 @@
import express from 'express';
import logger from 'morgan'
// import path from 'path'
import path from 'path'
const app = express();
import fs from 'fs'
import dotenv from 'dotenv'
// 检测运行目录下是否有 .env 文件,如果没有,则创建
const envPath = path.resolve(__dirname, '.env')
if (!fs.existsSync(envPath)) {
fs.writeFileSync(envPath, '')
// log
console.log('create .env file')
}
// 加载 .env 文件
dotenv.config()
console.log(process.env)
import indexRouter from './src/routes/index'
import usersRouter from './src/routes/users'

View File

@ -19,7 +19,7 @@
"typescript": "4.5.2"
},
"scripts": {
"start": "ts-node src/index.ts",
"start": "ts-node src/config/index.ts",
"typeorm": "typeorm-ts-node-commonjs"
}
}

View File

@ -1,6 +1,6 @@
import "reflect-metadata"
import { DataSource } from "typeorm"
import { User } from "./entity/User"
import { User } from "../entity/User"
export const AppDataSource = new DataSource({
type: "mysql",

View File

@ -1,5 +1,5 @@
import { AppDataSource } from "./data-source"
import { User } from "./entity/User"
import { User } from "../entity/User"
AppDataSource.initialize().then(async () => {