koa-yuekao
v1.0.1
Published
[![NPM version][npm-image]][npm-url] [![Build status][travis-image]][travis-url] [![Test coverage][coveralls-image]][coveralls-url] [![Dependency Status][david-image]][david-url] [![License][license-image]][license-url] [![Downloads][downloads-image]
Downloads
2
Readme
koa-static
Koa static file serving middleware, wrapper for koa-send
.
Installation
$ npm install koa-static
API
const Koa = require('koa');
const app = new Koa();
app.use(require('koa-static')(root, opts));
root
root directory string. nothing above this root directory can be servedopts
options object.
Options
router.post('/user/logon',indexController.Logon)
router.post('/user/sendcode',indexController.SendCode)
router.post('/user/login',indexController.Login)
router.get('/user/getUser', indexController.GetUser);
router.post('/order/add',userController.AddOrder)
router.get('/order/show',userController.OrderShow)
router.delete("/order/delete",userController.DeleteOrder)
router.post("/order/again",userController.AgainByShop)
router.put("/order/update",userController.UpdateStatus)
maxage
Browser cache max-age in milliseconds. defaults to 0hidden
Allow transfer of hidden files. defaults to falseindex
Default file name, defaults to 'index.html'defer
If true, serves afterreturn next()
, allowing any downstream middleware to respond first.gzip
Try to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested file with .gz extension exists. defaults to true.br
Try to serve the brotli version of a file automatically when brotli is supported by a client and if the requested file with .br extension exists (note, that brotli is only accepted over https). defaults to true.- setHeaders Function to set custom headers on response.
extensions
Try to match extensions from passed array to search for file when no extension is sufficed in URL. First found is served. (defaults tofalse
)
Example
const ChaeckPhone=async(params)=>{
var sql=` select * from userinfo where phone='${params.phone}' `
return await query(sql)
}
const AddUser=async(params)=>{
var newpassword=md5(params.password)
var sql=` insert into userinfo (account,password,phone) values
('${params.account}','${newpassword}','${params.phone}') `
return await query(sql)
}
See also
- koajs/conditional-get Conditional GET support for koa
- koajs/compress Compress middleware for koa
- koajs/mount Mount
koa-static
to a specific path
Options
maxage
Browser cache max-age in milliseconds. defaults to 0hidden
Allow transfer of hidden files. defaults to falseindex
Default file name, defaults to 'index.html'defer
If true, serves afterreturn next()
, allowing any downstream middleware to respond first.gzip
Try to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested file with .gz extension exists. defaults to true.br
Try to serve the brotli version of a file automatically when brotli is supported by a client and if the requested file with .br extension exists (note, that brotli is only accepted over https). defaults to true.- setHeaders Function to set custom headers on response.
extensions
Try to match extensions from passed array to search for file when no extension is sufficed in URL. First found is served. (defaults tofalse
)
Example
const ChaeckPhone = async (params) => {
var sql = ` select * from userinfo where account='${params.account}' or phone='${params.phone}' `
return await query(sql)
}
const AddUser = async (params) => {
var newpassword = md5(params.password)
var sql = ` insert into userinfo (account,password,phone)values
('${params.account}','${newpassword}','${params.phone}') `
return await query(sql)
}
const AddOrder = async (params) => {
var sql1 = ` select a.*,a.count*b.price zprice from car a join shop b on a.shopid=b.id where a.id in (${params.ids}) `
var caedata = await query(sql1)
if (caedata.length == 0) {
return {
affectedRows: 0
}
}
var sql2 = ` insert into orders (shopid,zprice,userid,ordercode,cretime,status) values`
for (let i = 0; i < caedata.length; i++) {
var shopid = caedata[i].shopid;
var userid = caedata[i].userid;
var zprice = caedata[i].zprice;
var ordercode = Math.random().toFixed(10).slice(-10)
var cretime = moment().format('YYYY-MM-DD HH:mm:ss');
sql2 += `('${shopid}','${zprice}','${userid}','${ordercode}','${cretime}','0'),`
}
sql2 = sql2.substring(0, sql2.length - 1)
var orderdata = await query(sql2)
if (orderdata.affectedRows > 0) {
var sql3 = ` delete from car where id in (${params.ids})`
return await query(sql3)
} else {
return {
affectedRows: 0
}
}
}
const OrderShow = async (params) => {
var page = params.page;
var size = params.size;
var sql = ` select a.*,b.shopname,b.price shopprice from orders a
join shop b on a.shopid=b.id where 1=1`
if (params.shopname) {
sql += ` and b.shopname like '%${params.shopname}%'`
}
if (params.status != -1 && params.status != undefined) {
sql += ` and a.status='${params.status}' `
}
var zcount = (await query(sql)).length;
sql += ` limit ${(page-1)*size},${size} `
var res = await query(sql)
return {res,zcount}
}
const DeleteOrder = async(params)=>{
var sql=` delete from orders where status='4' and id='${params.id}' `
return await query(sql2)
}
const AgainByShop=async(params)=>{
var sql1=` select * from orders where id='${params.id}' `
var orderdata=await query(sql1);
var createtime = moment().format('YYYY-MM-DD HH:mm:ss');
var ordercode = Math.random().toFixed(10).slice(-10)
var sql2=` insert into orders (ordercode,shopid,price,status,userid,count,createtime) values
('${ordercode}','${orderdata[0].shopid}','${orderdata[0].price}','0','${orderdata[0].userid}','${orderdata[0].count}','${createtime}')
`
return await query(sql2)
}
const UpdateStatus=async(params)=>{
var status=0;
if(params.status==0){
status=1
}else if(params.status==1){
status=2
}else if(params.status==2){
status=3
}else if(params.status==3){
status=4
}else{
return {affectedRows:0}
}
var sql=` update orders set status=${status} where id='${params.id}' `
return await query(sql)
}
See also
- koajs/conditional-get Conditional GET support for koa
- koajs/compress Compress middleware for koa
- koajs/mount Mount
koa-static
to a specific path
Options
maxage
Browser cache max-age in milliseconds. defaults to 0hidden
Allow transfer of hidden files. defaults to falseindex
Default file name, defaults to 'index.html'defer
If true, serves afterreturn next()
, allowing any downstream middleware to respond first.gzip
Try to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested file with .gz extension exists. defaults to true.br
Try to serve the brotli version of a file automatically when brotli is supported by a client and if the requested file with .br extension exists (note, that brotli is only accepted over https). defaults to true.- setHeaders Function to set custom headers on response.
extensions
Try to match extensions from passed array to search for file when no extension is sufficed in URL. First found is served. (defaults tofalse
)
Example
const AddOrder = async (ctx) => {
var res = await userServics.AddOrder(ctx.request.body);
if (res.affectedRows > 0) {
return ctx.body = {
code: 200,
message: ''
}
} else {
return ctx.body = {
code: 403,
message: ''
}
}
}
const OrderShow = async (ctx) => {
ctx.request.query.userid=ctx.state.user.data.id
var res = await userServics.OrderShow(ctx.request.query)
if (res.res.length > 0) {
return ctx.body = {
code: 200,
msg: '',
data:res
}
} else {
return ctx.body = {
code: 403,
msg: ''
}
}
}
const DeleteOrder = async (ctx) => {
var res = await userServics.DeleteOrder(ctx.request.query)
if (res.affectedRows > 0) {
return ctx.body = {
code: 200,
msg: ''
}
} else {
return ctx.body = {
code: 403,
msg: ''
}
}
}
const AgainByShop = async (ctx) => {
var res = await userServics.AgainByShop(ctx.request.body)
if (res.affectedRows > 0) {
return ctx.body = {
code: 200,
msg: ''
}
} else {
return ctx.body = {
code: 403,
msg: ''
}
}
}
const UpdateStatus = async (ctx) => {
var res = await userServics.UpdateStatus(ctx.request.body)
if (res.affectedRows > 0) {
return ctx.body = {
code: 200,
msg: ''
}
} else {
return ctx.body = {
code: 403,
msg: ''
}
}
}
See also
- koajs/conditional-get Conditional GET support for koa
- koajs/compress Compress middleware for koa
- koajs/mount Mount
koa-static
to a specific path
Options
maxage
Browser cache max-age in milliseconds. defaults to 0hidden
Allow transfer of hidden files. defaults to falseindex
Default file name, defaults to 'index.html'defer
If true, serves afterreturn next()
, allowing any downstream middleware to respond first.gzip
Try to serve the gzipped version of a file automatically when gzip is supported by a client and if the requested file with .gz extension exists. defaults to true.br
Try to serve the brotli version of a file automatically when brotli is supported by a client and if the requested file with .br extension exists (note, that brotli is only accepted over https). defaults to true.setHeaders Function to set custom headers on response.
extensions
Try to match extensions from passed array to search for file when no extension is sufficed in URL. First found is served. (defaults tofalse
)
Example
const Logon = async(ctx)=>{
var res = await indexServics.ChaeckPhone(ctx.request.body);
if(res.length>0){
return ctx.body={
code:403,
message:''
}
}
var res1 = await indexServics.AddUser(ctx.request.body);
if(res1.affectedRows>0){
return ctx.body={
code:200,
message:''
}
}else{
return ctx.body={
code:403,
message:''
}
}
}
const SendCode = async(ctx)=>{
var phone = ctx.request.body.phone
if(!phone){
return ctx.body={
code:403,
message:''
}
}
var checephone= /^1[3-9]\d{9}$/
if(!checephone){
return cyx.body={
code:403,
message:''
}
}
var rediscode = await redis.get(phone)
if(rediscode==null){
var code = Math.random().toFixed(6).slice(-6)
await redis.setex(phone,60,code);
return ctx.body={
code:200,
message:'',
data:code
}
}else{
return ctx.body={
code:403,
message:''
}
}
}
const Login = async(ctx)=>{
var res = await indexServics.ChaeckPhone(ctx.request.body)
if(res.length==0){
return ctx.body={
code:403,
message:''
}
}
var rediscode = await redis.get(ctx.request.body.phone)
if(rediscode==ctx.request.body.code){
var token = await jwt.signtoken(res[0])
return ctx.body={
code:200,
message:'',
data:'Bearer '+token
}
}else{
return ctx.body={
code:403,
message:''
}
}
}
const GetUser=(ctx)=>{
var userinfo=ctx.state.user.data;
return ctx.body = {
code: 200,
message: "",
data:[userinfo]
}
}
See also
- koajs/conditional-get Conditional GET support for koa
- koajs/compress Compress middleware for koa
- koajs/mount Mount
koa-static
to a specific path
CCCCCCAAAAARRRR
<body>
<div>
<span> </span><input type="text" id="phone">
<span> </span><input type="text" id="code">
<input type="button" value=" " onclick="SendCode()">
<input type="button" value=" " onclick="Login()">
</div>
<div id="data"></div>
</body>
<script>
const SendCode = async()=>{
var data={
phone:document.getElementById("phone").value
}
var res = await request.post('/user/sendcode',data);
if(res.data.code==200){
alert(res.data.data)
}else{
alert(res.data.message)
}
}
const Login = async()=>{
var data ={
phone:document.getElementById('phone').value,
code:document.getElementById('code').value
}
var res = await request.post('/user/login',data)
if(res.data.code==200){
localStorage.setItem('token',res.data.data)
location.href='/myWeb/Add.html';
}else{
alert(res.data.message)
}
}
</script>
</html>
See also
- koajs/conditional-get Conditional GET support for koa
- koajs/compress Compress middleware for koa
- koajs/mount Mount
koa-static
to a specific path
LOGIN
<body>
<input type="text" id="type" value="-1" hidden>
<div>
<span> </span><input type="text" id="shopname">
<input type="button" value=" " onclick="Query()">
</div>
<div>
<input type="button" value=" " onclick="Type(-1)">
<input type="button" value=" " onclick="Type(0)">
<input type="button" value=" " onclick="Type(1)">
<input type="button" value=" " onclick="Type(2)">
<input type="button" value=" " onclick="Type(3)">
<input type="button" value=" " onclick="Type(4)">
</div>
<table style="text-align: center;">
<thead>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
</thead>
<tbody id="data"></tbody>
</table>
<div>
<input type="button" value=" " onclick="Page(true)">
<input type="button" value=" " onclick="Page(false)">
</div>
</body>
<script>
var page = 1;
var size = 3;
var zcount = 0
var OrderShow = async () => {
var data = {
page,
size,
shopname: document.getElementById('shopname').value,
status: document.getElementById('type').value
}
var res = await request.get("/order/show", {
params: data
})
if (res.data.code == 200) {
document.getElementById('data').innerHTML = res.data.data.res.map((i) => {
return `
<tr>
<td>${i.ordercode}</td>
<td>${i.shopname}</td>
<td>${i.shopprice}</td>
<td>${i.zprice}</td>
<td>${i.status == 0 ? " " : i.status == 1 ? " " : i.status == 2 ? " " : i.status == 3 ? " " : " "}</td>
<td>${i.cretime.substring(0,10)}</td>
<td>
<input type="button" value=" " onclick="Details(${i.shopid})">
<input type="button" value=" " onclick="DeleteOrder(${i.id})">
<input type="button" value=" " onclick="Again(${i.id},${i.status})">
<input type="button" value=" " onclick="UpdateStatus(${i.id},${i.status})">
</td>
</tr>
`
}).join('')
zcount = res.data.data.zcount
}
}
OrderShow()
const Query = () => {
page = 1;
OrderShow()
}
const Page = (data) => {
if (data) {
if (page == 1) {
return alert(' ')
} else {
page = page - 1
}
} else {
if (page == Math.ceil(zcount / size)) {
return alert(' ')
} else {
page = page + 1
}
}
OrderShow()
}
const Type = (data) => {
document.getElementById('type').value = data
OrderShow()
}
const Details = (id) => {
location.href = '/myWeb/Details.html?iid' + id
}
const DeleteOrder = async (id) => {
var res = await request.delete('/order/delete', {
params: {
id
}
})
if (res.data.code == 200) {
OrderShow()
} else {
alert(res.data.message)
}
}
const Again = async (id, status) => {
if (status != 4) {
return alert(' ')
}
var res = await request.post('/order/again', {
id
})
if (res.data.code == 200) {
OrderShow()
} else {
alert(res.data.message)
}
}
const UpdateStatus = async (id, status) => {
if (status == 4) {
return alert(' ')
}
var res = await request.put('/order/update', {
id,
status
})
if (res.data.code == 200) {
OrderShow()
} else {
return alert(' ')
}
}
</script>
</html>
See also
- koajs/conditional-get Conditional GET support for koa
- koajs/compress Compress middleware for koa
- koajs/mount Mount
koa-static
to a specific path
License
MIT