Skip to content

Commit

Permalink
release v0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lixinyang123 committed Apr 21, 2024
1 parent 66713a8 commit 90bee63
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 80 deletions.
42 changes: 40 additions & 2 deletions lib/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,51 @@ export class Group {
this.routes = []
}

map(routePath, callback) {
/**
* Map endpoint
* @param {string} routePath endpoint path
* @param {Function} callback (req, res) => { }
* @param {string} method GET | POST | PUT | ...
* @returns {Plankton} Plankton app
*
* @example
* group.map('/', (req, res) => {
* res.end('hello world')
* })
*
* @example
* group.map('/hello', (req, res) => {
* res.end('hello world')
* }, 'GET')
*/
map(routePath, callback, method = '') {
this.routes.push({
path: path.join(this.basePath, routePath),
callback: callback
callback: callback,
method: method.toLowerCase()
})
}

/**
* Map endpoint group
* @param {string} routePath group base path
* @param {Function} callback group => { }
*
* @example
* app.mapGroup('/api', group => {
* group.mapGroup('v1', v1 => {
* // curl http://127.0.0.1/api/v1/hello
* v1.map('/hello', (req, res) => {
* res.end('hello world')
* })
* })
*
* // curl http://127.0.0.1/api/about
* group.mapGet('/about', (req, res) => {
* res.end('about')
* })
* })
*/
mapGroup(basePath, callback) {
if (!basePath.startsWith('/')) basePath = '/' + basePath
let group = new Group(basePath)
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware/cors.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export default async function (req, res, next) {
export default async function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*')
res.setHeader('Access-Control-Allow-Methods', '*')
res.setHeader('Access-Control-Allow-Headers', '*')
Expand Down
2 changes: 1 addition & 1 deletion lib/middleware/staticfile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from 'fs'
import path from 'path'

export default function (wwwroot = 'wwwroot', requestPath = '/') {
export default function(wwwroot = 'wwwroot', requestPath = '/') {
return async (req, res, next) => {

if (!requestPath.startsWith('/')) requestPath = `/${requestPath}`
Expand Down
75 changes: 0 additions & 75 deletions lib/plankton.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,81 +184,6 @@ export class Plankton {
return this
}

/**
* Map GET endpoint
* @param {string} routePath endpoint path
* @param {Function} callback (req, res) => { }
* @returns {Plankton} Plankton app
*
* @example
* app.mapGet('/', (req, res) => {
* res.end('hello world')
* })
*/
mapGet(routePath, callback) {
return this.map(routePath, callback, 'GET')
}

/**
* Map POST endpoint
* @param {string} routePath endpoint path
* @param {Function} callback (req, res) => { }
* @returns {Plankton} Plankton app
*
* @example
* app.mapPost('/', async (req, res) => {
* res.end('hello world')
* })
*/
mapPost(routePath, callback) {
return this.map(routePath, callback, 'POST')
}

/**
* Map PUT endpoint
* @param {string} routePath endpoint path
* @param {Function} callback (req, res) => { }
* @returns {Plankton} Plankton app
*
* @example
* app.mapPut('/', (req, res) => {
* res.end('hello world')
* })
*/
mapPut(routePath, callback) {
return this.map(routePath, callback, 'PUT')
}

/**
* Map PATCH endpoint
* @param {string} routePath endpoint path
* @param {Function} callback (req, res) => { }
* @returns {Plankton} Plankton app
*
* @example
* app.mapPatch('/', (req, res) => {
* res.end('hello world')
* })
*/
mapPatch(routePath, callback) {
return this.map(routePath, callback, 'PATCH')
}

/**
* Map DELETE endpoint
* @param {string} routePath endpoint path
* @param {Function} callback (req, res) => { }
* @returns {Plankton} Plankton app
*
* @example
* app.mapDelete('/', (req, res) => {
* res.end('hello world')
* })
*/
mapDelete(routePath, callback) {
return this.map(routePath, callback, 'DELETE')
}

/**
* Map endpoint group
* @param {string} routePath group base path
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lixinyang123/plankton",
"version": "0.1.16",
"version": "0.2.0",
"description": "Fast, Sample, Zero dependenecs Node.js mvc web framework",
"author": "lllxy",
"type": "module",
Expand Down

0 comments on commit 90bee63

Please sign in to comment.