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)
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 { 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 { Context, inject, controller, get, provide } from 'midway';
@provide()
@controller('/')
export class HomeController {
@inject()
ctx: Context;
@get('/')
async index() {
this.ctx.body = `Welcome to midwayjs!`;
}
}
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,
};
}
import { provide } from 'midway';
import { IUserOptions, IUserResult, IUserService } from '../../interface';
@provide('userService')
export class UserService implements IUserService {
async getUser(options: IUserOptions): Promise {
return new Promise((resolve) => {
setTimeout(() => {
resolve({
id: options.id,
username: 'mockedName',
phone: '12345678901',
email: 'xxx.xxx@xxx.com',
});
}, 100);
});
}
}
/**
* 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);
}
import { controller, get, provide } from 'midway';
@provide()
@controller('/')
export class HomeController {
@get('/')
async index(ctx) {
await ctx.render('index.ejs', {
assets: ctx.assets,
});
}
}
import { controller, get, post, provide, inject } from 'midway';
import { IPostService } from '../../interface';
@provide()
@controller('/post')
export class PostController {
@inject()
postService: IPostService;
@get('/')
async index(ctx) {
const query = {
limit: parseInt(ctx.query.limit, 10) || 10,
offset: parseInt(ctx.query.offset, 10) || 0,
};
ctx.body = await this.postService.list(query);
}
@get('/find')
import { controller, get, provide } from 'midway';
@provide()
@controller('/')
export class HomeController {
@get('/')
async index(ctx) {
ctx.body = `Welcome to midwayjs!`;
}
}