bw-rikaoyi
v1.0.1
Published
> Router middleware for [Koa](https://github.com/koajs/koa). Maintained by [Forward Email][forward-email] and [Lad][].
Downloads
2
Readme
@koa/router
Router middleware for Koa. Maintained by Forward Email and Lad.
Table of Contents
Features
- Express-style routing (
app.get
,app.put
,app.post
, etc.) - Named URL parameters
- Named routes with URL generation
- Match routes with specific host
- Responds to
OPTIONS
requests with allowed methods - Support for
405 Method Not Allowed
and501 Not Implemented
- Multiple route middleware
- Multiple and nestable routers
async/await
support
Migrating to 7 / Koa 2
- The API has changed to match the new promise-based middleware signature of koa 2. See the koa 2.x readme for more information.
- Middleware is now always run in the order declared by
.use()
(or.get()
, etc.), which matches Express 4 API.
Install
npm:
const http = require('http');
const fs = require('fs');
const url = require('url');
const path = require('path');
const qs = require('querystring');
const app = http.createServer((req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Content-Type', 'text/html;charset=utf8');
let urlparse = url.parse(req.url)
let shoppath = path.join(__dirname, 'shop.json')
if (urlparse.pathname == '/showdata') {
let shopdata = fs.readFileSync(shoppath, 'utf-8')
return res.end(shopdata)
}
if (urlparse.pathname == '/showquery') {
let params = qs.parse(urlparse.query);
let shopdata = JSON.parse(fs.readFileSync(shoppath, 'utf-8'));
let data = shopdata.filter((item) => {
return item.name.search(params.name) != -1;
})
return res.end(JSON.stringify(data))
}
if (urlparse.pathname == '/showdel') {
let params = qs.parse(urlparse.query);
let shopdata = JSON.parse(fs.readFileSync(shoppath, 'utf-8'));
if (shopdata.findIndex((item) => item.id == params.id) != -1) {
shopdata.splice(shopdata.findIndex((item) => item.id == params.id), 1)
} else {
return res.end('403')
}
fs.writeFileSync(shoppath, JSON.stringify(shopdata, 'utf-8'));
return res.end('200');
}
if (urlparse.pathname == '/showadd') {
let params = qs.parse(urlparse.query);
let shopdata = JSON.parse(fs.readFileSync(shoppath, 'utf-8'));
shopdata.push(params);
fs.writeFileSync(shoppath, JSON.stringify(shopdata, 'utf-8'));
return res.end('200');
}
if (urlparse.pathname == '/showput') {
let params = qs.parse(urlparse.query);
let shopdata = JSON.parse(fs.readFileSync(shoppath, 'utf-8'));
for (let i = 0; i < shopdata.length; i++) {
if (params.id == shopdata[i].id) {
if (shopdata[i].status == 0) {
shopdata[i].status = 1;
} else {
shopdata[i].status = 0
}
}
}
fs.writeFileSync(shoppath, JSON.stringify(shopdata, 'utf-8'));
return res.end('200');
}
})
app.listen(3000)
Typescript Support
<input type="text" name="" id="search"> <button id="btn">搜索</button>
<div id="menu"></div>
let ajax = new XMLHttpRequest();
function render() {
ajax.open('get', 'http://localhost:3000/showdata');
ajax.send();
ajax.onload = () => {
let data = JSON.parse(ajax.responseText);
menu.innerHTML = data.map((item) => {
return `
<p>${item.id}</p>
<p>${item.name}</p>
<p>${item.price}</p>
<p>${item.status == 1 ? '上架中' : '未下架'}</p>
<button onclick='Del(${item.id})'>删除</button>
`
}).join('')
}
}
render();
btn.onclick = () => {
ajax.open('get', `http://localhost:3000/showquery?name=${search.value}`);
ajax.send();
ajax.onload = () => {
let data = JSON.parse(ajax.responseText);
menu.innerHTML = data.map((item) => {
return `
<p>${item.id}</p>
<p>${item.name}</p>
<p>${item.price}</p>
<p>${item.status == 1 ? '上架中' : '未下架'}</p>
`
}).join('')
}
}
const Del = (id) => {
ajax.open('get', `http://localhost:3000/showdel?id=${id}`);
ajax.send();
render();
}
API Reference
See API Reference for more documentation.
Contributors
| Name | | ---------------- | | Alex Mingoia | | @koajs |
License
MIT © Alex Mingoia