Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { controller, get, provide, inject, Context } from 'midway'
import { Config } from 'ykfe-utils'
import renderToStream from 'ykfe-utils/lib/renderToStream'
import { IApiService } from '../../interface'
const ssrConfig: Config = require('../../../config/config.ssr')
@provide()
@controller('/')
export class Page {
@inject()
ctx: Context
@inject('ApiService')
service: IApiService
async index () {
try {
// Page为webpack打包的chunkName,项目默认的entry为Page
this.ctx.type = 'text/html'
this.ctx.status = 200
this.ctx.apiService = this.service.index // 将service挂载到上下文对象
const config = Object.assign(this.ctx.app.config, ssrConfig)
const stream = await renderToStream(this.ctx, config)
this.ctx.res.write('')
this.ctx.body = stream
} catch (error) {
this.ctx.logger.error(`Page Controller renderToStream Error`, error)
}
}
import { controller, provide, inject, Context, get } from 'midway'
import { IApiService } from '../../interface'
@provide()
@controller('/api')
export class Api {
@inject()
ctx: Context
@inject('ApiService')
service: IApiService
@get('/getIndexData')
async index () {
try {
// Page为webpack打包的chunkName,项目默认的entry为Page
this.ctx.type = 'text/json'
this.ctx.status = 200
this.ctx.body = await this.service.index()
} catch (error) {
this.ctx.logger.error(`Page Controller renderToStream Error`, error)
}
}
}
import { controller, get, inject } from 'midway';
import { IUserAbstract, IUserResult } from '../../lib/interfaces/user.abstract';
type nextDefinition = () => void;
@controller('/user/')
export class UserController {
@inject('userService')
service: IUserAbstract;
@get('/:id')
async getUser(ctx: any, next: nextDefinition): Promise {
const id: number = ctx.request.params('id');
const user: IUserResult = await this.service.getUser({id});
ctx.body = {success: true, message: 'OK', data: user};
// do not response again
await next();
}
}
import { controller, get, inject, provide } from 'midway';
import { IUserResult, IUserService } from '../../interface';
@provide()
@controller('/user')
export class UserController {
@inject('userService')
service: IUserService;
@get('/:id')
async getUser(ctx): Promise {
const id: number = ctx.params.id;
const user: IUserResult = await this.service.getUser({id});
ctx.body = {success: true, message: 'OK', data: user};
}
}
import { inject, provide } from 'midway';
import { IPostModel } from '../model/post';
import { IListQueryOpt, IListQueryOptions, IPostService } from './post.i';
@provide('postService')
export class PostService implements IPostService {
@inject('PostModel')
public model: IPostModel;
public async list({ offset = 0, limit = 10, title }: IListQueryOptions) {
const options: IListQueryOpt = {
offset,
limit,
attributes: ['id', 'title', 'content', 'created_at', 'updated_at'],
order: [['updated_at', 'desc'], ['id', 'desc']],
};
if (title) {
options.where = {
title,
};
}
return this.model.findAndCountAll(options);
}
import { controller, get, post, inject, provide } from 'midway';
import { IUserService } from '../../interface';
@provide()
@controller('/user')
export class UserController {
@inject('userService')
service: IUserService;
/**
* GET /user/profile
*/
@get('/profile')
async profile(ctx) {
const res = await this.service.profile();
ctx.body = res.data;
}
/**
* POST /user/login
*/
@post('/login')
async login(ctx) {
import { provide, inject } from 'midway';
import { IUserService, IUserProfile } from '../../interface';
@provide('userService')
export class UserService implements IUserService {
@inject('userModel')
model;
async profile(): Promise {
const data = await this.model.findUserProfile();
return { data };
}
}
import { controller, get, inject, provide } from 'midway';
import { IUserService, IUserResult } from '../../interface';
@provide()
@controller('/user')
export class UserController {
@inject('userService')
service: IUserService;
@get('/:id')
async getUser(ctx): Promise {
const id: number = ctx.params.id;
const user: IUserResult = await this.service.getUser({id});
ctx.body = {success: true, message: 'OK', data: user};
}
}
import { Context, controller, get, inject, provide } from 'midway';
import { IUserService, IUserResult } from '../../interface';
@provide()
@controller('/user')
export class UserController {
@inject()
ctx: Context;
@inject('userService')
service: IUserService;
@get('/:id')
async getUser(): Promise {
const id: number = this.ctx.params.id;
const user: IUserResult = await this.service.getUser({id});
this.ctx.body = {success: true, message: 'OK', data: user};
}
}
/**
* midway sequelize 使用范例
* 参考
* - https://github.com/midwayjs/midway-examples/tree/master/demo-sequelize
* - http://docs.sequelizejs.com/manual/typescript.html
*/
import { controller, del, get, inject, patch, post, provide } from 'midway';
import { IPostService } from '../../interface';
@provide()
@controller('/post/')
export class PostController {
@inject('postService')
public service: IPostService;
/**
* GET /post
*/
@get('/')
public async index(ctx) {
const query = {
limit: parseInt(ctx.query.limit, 10) || 10,
offset: parseInt(ctx.query.offset, 10) || 0,
};
ctx.body = await this.service.list(query);
}
/**
* GET /post/:id