改进 Express 模板
This commit is contained in:
parent
466bc6a0c6
commit
dd97d2bf95
@ -1,5 +1,7 @@
|
|||||||
|
PORT=3000
|
||||||
|
|
||||||
DB_HOST=127.0.0.1
|
DB_HOST=127.0.0.1
|
||||||
DB_PORT=3306
|
DB_PORT=3306
|
||||||
DB_DATABASE=auth
|
DB_DATABASE=test
|
||||||
DB_USERNAME=root
|
DB_USERNAME=root
|
||||||
DB_PASSWORD=
|
DB_PASSWORD=
|
@ -3,5 +3,5 @@
|
|||||||
Steps to run this project:
|
Steps to run this project:
|
||||||
|
|
||||||
1. Run `npm i` command
|
1. Run `npm i` command
|
||||||
2. Setup database settings inside `database.ts` file
|
2. Setup database settings inside `typeorm.ts` file
|
||||||
3. Run `npm start` command
|
3. Run `npm start` command
|
||||||
|
23
main.ts
23
main.ts
@ -1,21 +1,12 @@
|
|||||||
import express from 'express';
|
|
||||||
import logger from 'morgan'
|
|
||||||
import './src/config/env'
|
import './src/config/env'
|
||||||
|
|
||||||
const app = express();
|
import './src/routes/main'
|
||||||
|
import app from './src/config/express'
|
||||||
|
|
||||||
import indexRouter from './src/routes/index'
|
const port = process.env.PORT || 3000
|
||||||
import usersRouter from './src/routes/users'
|
|
||||||
|
|
||||||
app.use(logger('dev'));
|
|
||||||
app.use(express.json());
|
|
||||||
app.use(express.urlencoded({ extended: false }));
|
|
||||||
// app.use(express.static(path.join(__dirname, 'public')));
|
|
||||||
|
|
||||||
app.use('/', indexRouter);
|
|
||||||
app.use('/users', usersRouter);
|
|
||||||
|
|
||||||
let port = 3000
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
console.log(`App listening on port ${port}`)
|
console.log(`Server is running at http://0.0.0.0:${port}`)
|
||||||
})
|
console.log(`You can visit http://localhost:${port}`)
|
||||||
|
console.log(`Press CTRL-C to stop \n`)
|
||||||
|
})
|
@ -21,7 +21,7 @@
|
|||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "ts-node main.ts",
|
"start": "ts-node main.ts",
|
||||||
"typeorm": "typeorm-ts-node-commonjs",
|
"typeorm": "typeorm-ts-node-commonjs",
|
||||||
"migrate": "typeorm-ts-node-commonjs migration:run -d src/config/database.ts",
|
"migrate": "typeorm-ts-node-commonjs migration:run -d src/config/typeorm.ts",
|
||||||
"migrate:rollback": "typeorm-ts-node-commonjs migration:revert -d src/config/database.ts"
|
"migrate:rollback": "typeorm-ts-node-commonjs migration:revert -d src/config/typeorm.ts"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
11
src/config/express.ts
Normal file
11
src/config/express.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import express from 'express';
|
||||||
|
import logger from 'morgan'
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
|
||||||
|
app.use(logger('dev'));
|
||||||
|
app.use(express.json());
|
||||||
|
app.use(express.urlencoded({ extended: false }));
|
||||||
|
|
||||||
|
export default app;
|
||||||
|
|
@ -1,20 +0,0 @@
|
|||||||
import { AppDataSource } from "./database"
|
|
||||||
import { User } from "../entity/User"
|
|
||||||
|
|
||||||
AppDataSource.initialize().then(async () => {
|
|
||||||
|
|
||||||
console.log("Inserting a new user into the database...")
|
|
||||||
const user = new User()
|
|
||||||
user.firstName = "Timber"
|
|
||||||
user.lastName = "Saw"
|
|
||||||
user.age = 25
|
|
||||||
await AppDataSource.manager.save(user)
|
|
||||||
console.log("Saved a new user with id: " + user.id)
|
|
||||||
|
|
||||||
console.log("Loading users from the database...")
|
|
||||||
const users = await AppDataSource.manager.find(User)
|
|
||||||
console.log("Loaded users: ", users)
|
|
||||||
|
|
||||||
console.log("Here you can setup and run express / fastify / any other framework.")
|
|
||||||
|
|
||||||
}).catch(error => console.log(error))
|
|
7
src/config/manager.ts
Normal file
7
src/config/manager.ts
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
import typeorm from "./typeorm";
|
||||||
|
|
||||||
|
typeorm.initialize().then(async () => {
|
||||||
|
console.log("Datasource initialized.")
|
||||||
|
}).catch(error => console.log(error))
|
||||||
|
|
||||||
|
export default typeorm.manager
|
@ -1,20 +1,21 @@
|
|||||||
import "reflect-metadata"
|
import "reflect-metadata"
|
||||||
import { DataSource } from "typeorm"
|
import { DataSource } from "typeorm"
|
||||||
// import { User } from "../entity/User"/
|
|
||||||
|
|
||||||
import "./env"
|
import "./env"
|
||||||
|
|
||||||
export const AppDataSource = new DataSource({
|
const typeorm = new DataSource({
|
||||||
type: "mysql",
|
type: "mysql",
|
||||||
host: process.env.DB_HOST,
|
host: process.env.DB_HOST,
|
||||||
port: Number(process.env.DB_PORT),
|
port: Number(process.env.DB_PORT),
|
||||||
username: process.env.DB_USERNAME,
|
username: process.env.DB_USERNAME,
|
||||||
password: process.env.DB_PASSWORD,
|
password: process.env.DB_PASSWORD,
|
||||||
database: process.env.DB_DATABASE,
|
database: process.env.DB_DATABASE,
|
||||||
synchronize: true,
|
synchronize: false,
|
||||||
logging: false,
|
logging: false,
|
||||||
// entities: [User],
|
|
||||||
entities: ["src/entity/*.ts"],
|
entities: ["src/entity/*.ts"],
|
||||||
migrations: ["src/migration/*.ts"],
|
migrations: ["src/migration/*.ts"],
|
||||||
subscribers: [],
|
subscribers: [],
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export default typeorm
|
||||||
|
|
||||||
|
|
29
src/controller/User.ts
Normal file
29
src/controller/User.ts
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
import {Request, Response} from "express";
|
||||||
|
import {User} from "../entity/User";
|
||||||
|
import manager from "../config/manager";
|
||||||
|
|
||||||
|
class UserController {
|
||||||
|
public async index(request: Request, response: Response) {
|
||||||
|
|
||||||
|
|
||||||
|
const user = new User()
|
||||||
|
user.name = "Timber"
|
||||||
|
user.email = "Saw@test.test"
|
||||||
|
await manager.save(user)
|
||||||
|
|
||||||
|
user.name = "update"
|
||||||
|
await manager.save(user)
|
||||||
|
|
||||||
|
return response.json({
|
||||||
|
message: 'Hello World! From user'
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
public async create(request: Request, response: Response) {
|
||||||
|
return response.json({
|
||||||
|
message: 'Hello World! From user'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default UserController
|
@ -1,6 +1,7 @@
|
|||||||
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
|
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"
|
||||||
|
|
||||||
@Entity()
|
@Entity("users")
|
||||||
|
|
||||||
export class User {
|
export class User {
|
||||||
|
|
||||||
@PrimaryGeneratedColumn()
|
@PrimaryGeneratedColumn()
|
||||||
|
@ -3,7 +3,11 @@ const router = Router()
|
|||||||
|
|
||||||
/* GET home page. */
|
/* GET home page. */
|
||||||
router.get('/', function(req, res, next) {
|
router.get('/', function(req, res, next) {
|
||||||
// res.render('index', { title: 'Express' });
|
|
||||||
|
|
||||||
|
// user
|
||||||
|
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
message: 'Hello World!'
|
message: 'Hello World!'
|
||||||
})
|
})
|
||||||
|
9
src/routes/main.ts
Normal file
9
src/routes/main.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
// 添加其他的路由文件
|
||||||
|
import app from "../config/express";
|
||||||
|
import indexRouter from "./index";
|
||||||
|
import usersRouter from "./users";
|
||||||
|
|
||||||
|
app.use('/', indexRouter);
|
||||||
|
app.use('/users', usersRouter);
|
||||||
|
|
||||||
|
|
@ -1,9 +1,11 @@
|
|||||||
import {Router} from 'express'
|
import {Router} from 'express'
|
||||||
const router = Router()
|
const router = Router()
|
||||||
|
|
||||||
|
import UserController from '../controller/User'
|
||||||
|
|
||||||
|
const user = new UserController()
|
||||||
|
|
||||||
/* GET users listing. */
|
/* GET users listing. */
|
||||||
router.get('/', function(req, res, next) {
|
router.get('/', user.index);
|
||||||
res.send('respond with a resource');
|
|
||||||
});
|
|
||||||
|
|
||||||
export default router
|
export default router
|
Loading…
Reference in New Issue
Block a user