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, 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, provide } from 'midway';
@provide()
@controller('/')
export class HomeController {
@get('/')
async index(ctx) {
ctx.body = `Welcome to midwayjs!`;
}
}
@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')
async show(ctx) {
ctx.body = await this.postService.find(parseInt(ctx.query.id, 10));
}
@post('/create')
async create(ctx) {
ctx.body = await this.postService.create(ctx.request.body);
}
@post('/update')
async update(ctx) {
const id = parseInt(ctx.request.body.id, 10);
ctx.body = await this.postService.update(
id,
ctx.request.body.updates,
);
import { controller, get, provide, Context } from 'midway';
@provide()
@controller('/')
export class HomeController {
@get('/')
async index(ctx: Context) {
ctx.body = ctx.getName() + ' ' + ctx.name;
}
@get('/api', {middleware: ['apiMiddleware']})
async api(ctx: Context) {
ctx.body = ctx.api;
}
}
import { controller, get, provide, Context } from 'midway';
@provide()
@controller('/')
export class HomeController {
@get('/')
async index(ctx: Context) {
ctx.body = ctx.getName() + ' ' + ctx.name;
}
@get('/api', {middleware: ['apiMiddleware']})
async api(ctx: Context) {
ctx.body = ctx.api;
}
}
import { Context, inject, controller, get, provide } from 'midway';
@provide()
@controller('/')
export class HomeController {
@inject()
ctx: Context;
@get('/')
async index() {
await this.ctx.render('index');
}
}
import { controller, get, provide } from 'midway';
@provide()
@controller('/')
export class HomeController {
@get('/')
async index(ctx) {
ctx.body = `Welcome to midwayjs!`;
}
}
* - 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
*/
@get('/:id')
public async show(ctx) {
ctx.body = await this.service.find(parseInt(ctx.params.id, 10));
}
import { controller, get, provide } from 'midway';
@provide()
@controller('/')
export class HomeController {
@get('/')
async index(ctx) {
await ctx.render('index.ejs', {
assets: ctx.assets,
});
}
}